我有一个通用的plotting类,它使用matplotlib生成(png)绘图,它可能有多个y轴,但始终只有一个显示日期的(共享)x轴。在
这是处理x轴标签格式的方法:
def format_xaxis(self, axis, primary):
steps = (1,2,3,4,6,12)
step = steps[min(len(self.dates) // 1000, 5)]
axis.set_axisbelow(True)
axis.xaxis.grid(b=True, which='minor', color='0.90', linewidth=0.5)
axis.xaxis.set_minor_locator(MonthLocator(bymonth=range(1,13,step)))
axis.xaxis.set_major_locator(YearLocator())
if primary:
axis.xaxis.set_major_formatter(DateFormatter(fmt='%b %y'))
axis.xaxis.set_minor_formatter(DateFormatter(fmt='%b'))
else:
plt.setp(axis.get_xticklabels(), visible=False)
输入:
主标签和主标签上的月份和年是唯一的标签。在
结果是主轴上只显示主标签,根本不显示副标签。在
如果我将最后6行改为:
^{pr2}$然后小标签显示在所有轴上。在
如何仅在主x轴上显示小x轴刻度标签?
编辑:
使用KevinG关于第二个代码块的建议:
axis.xaxis.set_major_locator(YearLocator())
axis.xaxis.set_major_formatter(DateFormatter(fmt='%b %y'))
axis.xaxis.set_minor_formatter(DateFormatter(fmt='%b'))
if not primary:
plt.setp(axis.get_xticklabels(minor=False), visible=False)
plt.setp(axis.get_xticklabels(minor=True), visible=False)
我注意到很多tick label的东西都有
minor=False
作为默认参数。如果现在没有多轴绘图,我只能建议你看看那里。我想象着应该会有效果的。在
相关问题 更多 >
编程相关推荐