2014-01-15 31 views
16

동일한 페이지에 세 개의 서브 그림을 플로팅합니다. 나는 모든 subplots을 통해 horiontal 라인을 그리기를 원한다. 내 코드와 결과 그래프는 다음과 같은 : 나는 다시 enter image description herepyplot을 사용하여 파이썬에서 여러 개의 서브 그림에 수평선을 그립니다.

: 이것은 다음을 생성

gs1 = gridspec.GridSpec(8, 2) 
gs1.update(left=0.12, right=.94, wspace=0.12) 
ax1 = plt.subplot(gs1[0:2, :]) 
ax2 = plt.subplot(gs1[3:5, :], sharey=ax1) 
ax3 = plt.subplot(gs1[6:8, :], sharey=ax1) 

ax1.scatter(theta_cord, density, c = 'r', marker= '1') 
ax2.scatter(phi_cord, density, c = 'r', marker= '1') 
ax3.scatter(r_cord, density, c = 'r', marker= '1') 
ax1.set_xlabel('Theta (radians)') 
ax1.set_ylabel('Galaxy count') 
ax2.set_xlabel('Phi (radians)') 
ax2.set_ylabel('Galaxy count') 
ax3.set_xlabel('Distance (Mpc)') 
ax3.set_ylabel('Galaxy count') 
plt.ylim((0,0.004)) 
loc = plticker.MultipleLocator(base=0.001) 
ax1.yaxis.set_major_locator(loc) 

plt.axhline(y=0.002, xmin=0, xmax=1, hold=None) 

plt.show() 

(당신은 내가 플롯 중 하나에서 수평선을 얻을 수 있지만 모든 수 알 수 있습니다) 마지막 서브 플로트에 그린 선을 처음 두 개의 서브 플로트에도 표시하고 싶습니다. 어떻게해야합니까?

+0

덜 귀하의 질문에 귀하의 축 레이블, 귀하의 실제 데이터와 같은 불필요한 세부 정보가 포함되어 있습니다. 코드가 복사하여 붙여 넣기를 실행할 수있는 것이 가장 좋습니다. – tacaswell

답변

30

나는 어쨌든 이것에 비틀 거린 누군가를 위해 그것을하는 방법을 찾아 냈다.

ax1.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0) 
ax2.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0) 
ax3.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0) 

이 생산 : 우리는 그것을 대체

plt.axhline(y=0.002, xmin=0, xmax=1, hold=None) 

:

우리는 OP에서 다음 줄을 교체 할 필요가 일반적으로

enter image description here

+2

당신을 풀어 줄 때 당신 자신의 답변을 수락하는 것을 잊지 마십시오. pyplot 대 OO 인터페이스에 대한 설명은 https://stackoverflow.com/questions/16849483/which-is-the-recommended-way-to-plot-matplotlib-or-pylab/16849816#16849816을 참조하십시오. . – tacaswell