2012-10-18 7 views
4

동일한 데이터를 두 가지 형식, 즉 로그 배율 및 선형 배율로 플롯합니다.동일한 '설정'을 가진 matplotlib 서브 플로트

기본적으로 나는 똑같은 음모가 있지만 다른 음계가 하나씩 다른 음절을 사용하고 싶습니다.

import matplotlib.pyplot as plt 

# These are the plot 'settings' 
plt.xlabel('Size') 
plt.ylabel('Time(s)'); 
plt.title('Matrix multiplication') 

plt.xticks(xl, rotation=30, size='small') 
plt.grid(True) 

# Settings are ignored when using two subplots 

plt.subplot(211) 
plt.plot(xl, serial_full, 'r--') 
plt.plot(xl, acc, 'bs') 
plt.plot(xl, cublas, 'g^') 

plt.subplot(212) 
plt.yscale('log') 
plt.plot(xl, serial_full, 'r--') 
plt.plot(xl, acc, 'bs') 
plt.plot(xl, cublas, 'g^') 

모든 '설정'

plt.subplot 전에이 무시됩니다

은 내가 지금해야하는 것은 이것이다.

내가 원하는대로 작동하도록 할 수는 있지만 각 서브 플로트 선언 후에 모든 설정을 복제해야합니다.

두 서브 플로트를 동시에 구성하는 방법이 있습니까?

답변

10

plt.* 설정은 보통 matplotlib의 현재 플로트에 적용됩니다. plt.subplot으로 새 플롯을 시작하면 설정이 더 이상 적용되지 않습니다. 플롯 (see examples here)과 연결된 Axes 오브젝트를 통해 레이블, 틱 (ticks) 등을 공유 할 수 있지만 IMHO는 여기에 지나치게 과장됩니다. 대신, 하나의 함수에 공통의 "스타일"을 넣어 제안하고 플롯 당이 부를 것이다 : 보조 노트에

def applyPlotStyle(): 
    plt.xlabel('Size') 
    plt.ylabel('Time(s)'); 
    plt.title('Matrix multiplication') 

    plt.xticks(range(100), rotation=30, size='small') 
    plt.grid(True) 

plt.subplot(211) 
applyPlotStyle() 
plt.plot(xl, serial_full, 'r--') 
plt.plot(xl, acc, 'bs') 
plt.plot(xl, cublas, 'g^') 

plt.subplot(212) 
applyPlotStyle() 
plt.yscale('log') 
plt.plot(xl, serial_full, 'r--') 
plt.plot(xl, acc, 'bs') 
plt.plot(xl, cublas, 'g^') 

을, 당신이 당신의 음모를 추출하여 더 많은 중복을 근절 할 수는 함수에 명령 :

def applyPlotStyle(): 
    plt.xlabel('Size') 
    plt.ylabel('Time(s)'); 
    plt.title('Matrix multiplication') 

    plt.xticks(range(100), rotation=30, size='small') 
    plt.grid(True) 

def plotSeries(): 
    applyPlotStyle() 
    plt.plot(xl, serial_full, 'r--') 
    plt.plot(xl, acc, 'bs') 
    plt.plot(xl, cublas, 'g^') 

plt.subplot(211) 
plotSeries() 

plt.subplot(212) 
plt.yscale('log') 
plotSeries() 

다른 쪽지에서는 (예 : suptitle을 사용하여) 그림의 맨 위에 제목을 넣으면됩니다.

def applyPlotStyle(): 
    plt.ylabel('Time(s)'); 

    plt.xticks(range(100), rotation=30, size='small') 
    plt.grid(True) 

def plotSeries(): 
    applyPlotStyle() 
    plt.plot(xl, serial_full, 'r--') 
    plt.plot(xl, acc, 'bs') 
    plt.plot(xl, cublas, 'g^') 

plt.suptitle('Matrix multiplication') 
plt.subplot(211) 
plotSeries() 

plt.subplot(212) 
plt.yscale('log') 
plt.xlabel('Size') 
plotSeries() 

plt.show() 
3

한스 '대답은 아마 권장되는 방법입니다 다음 xlabel는 두 번째 플롯 아래에 표시하는 Similary, 충분한 수 있습니다.

fig = figure() 
ax1 = fig.add_subplot(2,1,1) 
ax1.plot([1,2,3],[4,5,6]) 
title('Test') 
xlabel('LabelX') 
ylabel('Labely') 

ax2 = fig.add_subplot(2,1,2) 
ax2.plot([4,5,6],[7,8,9]) 


for prop in ['title','xlabel','ylabel']: 
    setp(ax2,prop,getp(ax1,prop)) 

show() 
fig.show() 

enter image description here

이 당신에 속성에 대한 화이트리스트를 설정할 수 있습니다 : 그러나 경우에 당신이 또 다른 축에 축 속성을 복사에 대한 가고 싶어, 여기 내가 찾은 방법입니다 지금은 title, xlabelylabel이지만, 사용 가능한 모든 속성의 목록을 인쇄하려면 getp(ax1)을 사용하면됩니다.

모두을 다음과 같이 사용하여 복사 할 수 있지만 일부 속성 설정이 두 번째 플롯을 엉망으로 만들 수 있으므로 권장하지 않습니다. 나는 몇 가지를 제외하기 위해 블랙리스트를 사용하려고했지만, 당신은 작업을 얻을 그것으로 바이올린 필요가 있습니다 :

insp = matplotlib.artist.ArtistInspector(ax1) 
props = insp.properties() 
for key, value in props.iteritems(): 
    if key not in ['position','yticklabels','xticklabels','subplotspec']: 
     try: 
      setp(ax2,key,value) 
     except AttributeError: 
      pass 

합니다 (except/pass은 getTable이있는 속성을 건너 뛸 수 있지만 설정할 수 없습니다)

+1

예기치 않은 오류가 발생하지 않도록 try 블록을 편집했습니다. (편집 대기열에 있음). – Joooeey

관련 문제