2012-10-16 3 views
17

보조 Y 축 (트윈스을 사용하여 생성)이 포함 된 여러 개의 서브 플로트가있는 경우 보조 플롯간에 보조 Y 축을 어떻게 공유 할 수 있습니까? 나는 그들이 자동적으로 똑같이 확장되기를 바란다. (나중에 y 한계를 수동으로 설정하지 않는다.) 기본 y 축의 경우 sharey서브 플롯으로 호출하여 사용할 수 있습니다.matplotlib에서 보조 플롯 간의 보조 y 축 공유 방법

아래 예제는 내 시도를 보여 주지만 두 subplots의 보조 y 축을 공유하지 못합니다. 나는하기 matplotlib/Pylab을 사용하고 있습니다 :

ax = [] 

#create upper subplot 
ax.append(subplot(211)) 
plot(rand(1) * rand(10),'r') 

#create plot on secondary y-axis of upper subplot 
ax.append(ax[0].twinx()) 
plot(10*rand(1) * rand(10),'b') 

#create lower subplot and share y-axis with primary y-axis of upper subplot 
ax.append(subplot(212, sharey = ax[0])) 
plot(3*rand(1) * rand(10),'g') 

#create plot on secondary y-axis of lower subplot 
ax.append(ax[2].twinx()) 
#set twinxed axes as the current axes again, 
#but now attempt to share the secondary y-axis 
axes(ax[3], sharey = ax[1]) 
plot(10*rand(1) * rand(10),'y') 

이 나에게 같은 것을 가져옵니다

Example of two subplots with failed sharing of secondary y-axis

나는 축을 사용하는 이유() 기능을 공유 y 축이 있다는 것입니다 설정 twinxsharey 키워드를 허용하지 않습니다.

저는 Win7 x64에서 Python 3.2를 사용하고 있습니다. Matplotlib 버전은 1.2.0rc2입니다.

답변

30

당신과 같이 Axes.get_shared_y_axes()를 사용할 수 있습니다 여기에

from numpy.random import rand 
import matplotlib 
matplotlib.use('gtkagg') 
import matplotlib.pyplot as plt 

# create all axes we need 
ax0 = plt.subplot(211) 
ax1 = ax0.twinx() 
ax2 = plt.subplot(212) 
ax3 = ax2.twinx() 

# share the secondary axes 
ax1.get_shared_y_axes().join(ax1, ax3) 

ax0.plot(rand(1) * rand(10),'r') 
ax1.plot(10*rand(1) * rand(10),'b') 
ax2.plot(3*rand(1) * rand(10),'g') 
ax3.plot(10*rand(1) * rand(10),'y') 
plt.show() 

우리는 단지 함께 보조 축에 합류하고 있습니다.

희망이 있습니다.

+0

네, 저에게 도움이되었습니다. 감사합니다. 또한 기본 y 축을 결합하여 y 축을 공유하도록했습니다. – Puggie

+0

기꺼이 도와 드리겠습니다. 인터넷 포인트를 가져 주셔서 감사합니다. – dmcdougall

+1

두 번째 트윈 눈금은 업데이트되지 않지만 두 번째 트윈 눈금은 그대로 유지됩니다. – Mattia