【发布时间】:2016-05-24 15:41:47
【问题描述】:
我希望你能帮我解决这个小问题。
我正在绘制三组误差线数组,我想在图的顶部有第二组刻度。所以我创建了一个新的双轴,我为刻度提供相同的坐标,x. 应该执行此操作的代码部分如下
x = np.arange(len(Stars))*2
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax1.errorbar(x-.5,[Jmin_IS[i] for i in Ind],yerr=[[J1sL_IS[i] for i in Ind],[J1sR_IS[i] for i in Ind]],fmt='o',ms=6)
ax1.errorbar(x ,[Jmin_OM[i] for i in Ind],yerr=[[J1sL_OM[i] for i in Ind],[J1sR_OM[i] for i in Ind]],fmt='s',ms=6)
ax1.errorbar(x+.5,[Jmin_CA[i] for i in Ind],yerr=[[J1sL_CA[i] for i in Ind],[J1sR_CA[i] for i in Ind]],fmt='^',ms=6)
for xv in np.arange(len(Stars))*4: ax1.axvspan(xv-1, xv+1, color='grey',alpha=.5)
x2labels = [str(r'N$_{\star}$=%i'%Stars[i]) for i in Ind]
ax1.set_xticks(x)
ax2.set_xticks(x)
ax2.set_xticklabels(x2labels, rotation=90)
ax1.set_ylim(15,21)
ax1.set_xlim(-1,39)
plt.tight_layout()
plt.grid(ls='dotted',axis='y')
plt.show()
结果如图
有两个问题:首先双轴 ax2 刻度发生偏移(N_star 数字相对于底部 x 轴的刻度未对齐)并且 y 方向没有显示网格。
有人能发现错误吗?是python包版本的问题吗?非常感谢
【问题讨论】:
标签: matplotlib axes