设置xticklabel重叠旧的xtick

时间:2018-03-27 10:26:46

标签: python matplotlib

我正在尝试修改绘图的x刻度,但是matplotlib自动生成的刻度标签之一不会消失,而我指定的刻度标签会重叠。代码是:

ax[0].plot((zspace+1.),dndt_vs_z_9, color='blue', label=r'$M_s = 10^9$')
ax[0].plot((zspace+1.),dndt_vs_z_10, color='red', label=r'$M_s = 10^{10}$')
ax[0].plot((zspace[zspace<=2]+1.),dndt_vs_z_11[zspace<=2], color='green', label=r'$M_s = 10^{11}$')
ax[0].plot((zspace[zspace<=2]+1.),(1./2.)*dndt_vs_z_11[zspace<=2], color='black')
ax[0].plot((zspace[zspace<=2]+1.),0.65*dndt_vs_z_11[zspace<=2], color='black')
ax[0].plot(gomez_11_x,gomez_11_y,color='blue', linestyle='dashed')
ax[0].plot(gomez_10_x,gomez_10_y,color='red', linestyle='dashed')
ax[0].plot(gomez_9_x,gomez_9_y,color='green', linestyle='dashed')
#ax[0].scatter(ill_shmr_z,ill_shmr_dndw_model_11)

ax[0].set_yscale('log')
ax[0].set_xscale('log')

ax[0].set_xticks([1,2,3,4,5], minor=True)
ax[0].set_xticklabels([1,2,3,4,5], minor=True, fontsize='20')
ax[0].set_xlim([1,5])
ax[0].set_ylim([1e-2,1e1])
ax[0].legend(loc='best', fontsize='20')

ax[0].grid(b = True, which='major')
ax[0].grid(b = True, which='minor', axis='x')
ax[0].set_xlabel(r'$1+z$', fontsize='15')
ax[0].set_ylabel(r'$\frac{dN}{dt}$', fontsize='20')

结果是:

Result

1 个答案:

答案 0 :(得分:2)

这里的问题来自于使用对数刻度时混合主要和次要刻度。您的示例案例中的解决方案是使用NullLocator删除主要刻度并仅使用次要刻度:

一个小例子是:

lubridate

enter image description here