2017-03-27 1 views
1

저는 matplotlib을 처음 사용하고있어서 약간의 문제가 있습니다. 나는 3 개의 플롯을 x 축과 2 개의 다른 y 축을 공유하면서 서로 쌓아 쌓으려고합니다. 이 내 현재 코드Matplotlib : xticks 레이블이 표시되지 않습니다.

import numpy as np 
import matplotlib.pyplot as plt 

periods = range(1,13) 
n0 = [4.2, 6.7, 10.6, 51.3, 339, 45.6, 56.3, 112.9, 182.7, 185.7, 126.2, 25.39] 
alp = [2.12, 2.14, 2.19, 2.35, 2.54, 2.33, 2.34, 2.43, 2.45, 2.46, 2.466, 2.249] 
B = [0.045, 0.041, 0.04, 0.04, 0.057, 0.048, 0.044, 0.057, 0.054, 0.065, 0.06, 0.045] 
emin = [166, 201.9, 215, 270.7, 351.8, 263.7, 302.2, 323.6, 328.7, 346.1, 279.5, 259.8] 
emax = [21806, 28407, 5706, 22087, 17978, 11699, 19440, 17988, 26938, 14812, 14195, 26121] 
eq = [7.8, 11.8, 13.3, 15.2, 8.87, 10.5, 13.8, 7.6, 11.5, 7.4, 6.4, 13.5] 

f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True) 
ax1.scatter(periods, emin, c="k") 
ax1.set_ylabel(r"$E_{min}") 
ax1.yaxis.set_ticks(range(160, 380, 40)) 

ax4 = ax1.twinx() 
ax4.scatter(periods, n0, c="r") 
ax4.set_ylabel(r"$N_0$", color = 'r') 
ax4.tick_params(colors = 'r') 
ax4.yaxis.set_ticks(np.arange(20, 340, 50)) 

ax2.scatter(periods, emax, c="k") 
ax2.set_ylabel(r"$E_{max}$") 
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000)) 
ax5 = ax2.twinx() 
ax5.scatter(periods, alpha, c="r") 
ax5.set_ylabel("alp", color = 'r') 
ax5.tick_params(colors = 'r') 
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1)) 

ax3.scatter(periods, eq, c="k") 
ax3.set_ylabel("Eq") 
ax3.yaxis.set_ticks(np.arange(6, 15, 2)) 
ax3.set_xlabel("Periods") 
ax6 = ax3.twinx() 
ax6.scatter(periods, B, c="r") 
ax6.set_ylabel("B", color = 'r') 
ax6.tick_params(colors = 'r') 
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02)) 
ax6.xaxis.set_ticks(range(1,13)) 

f.subplots_adjust(hspace = 0) 
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False) 
plt.show() 

하지만 x 축 레이블이 표시되지 않습니다. 레이블 자체 ("기간")는 괜찮지 만 틱은 없습니다. 회선을 변경하려고 시도했지만 다른 모든 숫자와 줄을 바꾸려고 시도했지만 다른 모든 숫자와 줄을 바꾸려고 시도했습니다. 어떤 도움이 필요합니까? y 축 레이블을 90도 회전하여 가로가되도록하는 방법은 무엇입니까? 나는 ylabel에 rotation = 90을 포함 시키려고했지만 다른 값이 작동하더라도 변경되지 않습니다.

덕분에 많은

편집 : 내가 지울 경우 twinx이 당신의 X 위해서는 미세

+0

두 번째 줄부터 마지막 ​​줄까지 확인하십시오 .....'visible = False'? – DavidG

답변

1

를 작동하면 마지막 줄에 두 번째를 변경해야하고 visible = True을 설정 표시 틱 축, 추가 잊어 버렸 . y 라벨을 90도 회전하려면 rotation =을 사용하고 회전을 0으로 설정합니다. 이렇게하면 레이블과 틱이 겹칠 수 있습니다. ax.set_ylabel()에서 labelpad =을 사용하여 삭제할 수 있습니다. 축과 레이블 사이의 간격입니다.

f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True) 
ax1.scatter(periods, emin, c="k") 
ax1.set_ylabel(r"$E_{min}$",rotation=0,labelpad=20) 
ax1.yaxis.set_ticks(range(160, 380, 40)) 

ax4 = ax1.twinx() 
ax4.scatter(periods, n0, c="r") 
ax4.set_ylabel(r"$N_0$", color = 'r',rotation=0,labelpad=10) 
ax4.tick_params(colors = 'r') 
ax4.yaxis.set_ticks(np.arange(20, 340, 50)) 

ax2.scatter(periods, emax, c="k") 
ax2.set_ylabel(r"$E_{max}$",rotation=0,labelpad=20) 
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000)) 
ax5 = ax2.twinx() 
ax5.scatter(periods, alp, c="r") 
ax5.set_ylabel("alp", color = 'r',rotation=0,labelpad=15) 
ax5.tick_params(colors = 'r') 
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1)) 

ax3.scatter(periods, eq, c="k") 
ax3.set_ylabel("Eq",rotation=0,labelpad=20) 
ax3.yaxis.set_ticks(np.arange(6, 15, 2)) 
ax3.set_xlabel("Periods") 
ax6 = ax3.twinx() 
ax6.scatter(periods, B, c="r") 
ax6.set_ylabel("B", color = 'r',rotation=0,labelpad=10) 
ax6.tick_params(colors = 'r') 
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02)) 
ax1.xaxis.set_ticks(np.arange(1, 13, 1)) 


f.subplots_adjust(hspace = 0,left=0.14,right=0.90) 
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=True) 
plt.show() 

이 다음 플롯이 생성

enter image description here

당신은 당신이 당신의 눈금 라벨의 길이로 원하는 위치에 정확하게 라벨을 얻기 위해 labelpad 실험 할 수는 동일하지 않습니다 각 축마다.

+0

고마워,이 효과. twix 명령을 추가 할 때 왜 변경 되었습니까? 필자가 이해 한대로,'plt.setp ([a.get_xticklabels() for a f.axes [: - 1]], visible = False)'마지막 축 레이블을 제외한 모든 x 축 눈금 레이블을 숨 깁니다.). 그리고 이것은 두 축없이 작동합니다 – bernie

+0

그래서 subplot 1과 2의 틱은 실제로 존재하지만,'hspace = 0' 때문에 맨 아래의 것들만 볼 수 있습니다. 원하는 출력을 생성 할 때 이것은 별 문제가 아니지만 쌍축이있는 이유는 모르겠습니다. – DavidG

+0

아니면 plt.setp ([f.axes [: - 1]]의 a.get_xticklabels(), visible = False)가 첫 번째 두 개의 서브 그림의 눈금을 숨기지 않는 이유를 모르겠습니다. 이 경우 – DavidG

관련 문제