首页 > 解决方案 > 1 月次要刻度标签未在 matplotlib 中显示

问题描述

我正在绘制一个时间序列的数据,并且有一年的主要刻度线和月份的次要刻度线。一切看起来都很好,除了一月份的“01”在一年中没有出现。有人对如何解决这个问题有任何建议吗?谢谢!

代码和相关图形输出如下。


fig=plt.figure(figsize=(40,30))
ax1 = plt.subplot(411)
plt.title('Moving Window Plot: Downsview 2015-2020',size=30)
ax2 = plt.subplot(412)
ax3 = plt.subplot(413)


ax1.errorbar(ym_grp.index,ym_grp.slope, yerr=ym_grp_sem.slope, ls='none',marker = 'o')
ax2.errorbar(ym_grp.index,ym_grp.yint, yerr = ym_grp_sem.yint, ls='none',marker = 'o')
ax3.errorbar(ym_grp.index,ym_grp.r2, yerr =ym_grp_sem.r2, ls='none', marker = 'o')

ax1.xaxis.set_major_locator(matplotlib.dates.YearLocator())
ax1.xaxis.set_minor_locator(matplotlib.dates.MonthLocator())

ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("\n%Y"))
ax1.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter("%m"))
plt.setp(ax1.get_xticklabels(), rotation=0, ha="center")

ax3.set_xlabel('Time (UTC)',fontsize=20)
ax1.set_xlim()

ax1.set_ylabel('ΔCO/ΔCO₂',fontsize=20)
ax2.set_ylabel('offset',fontsize=20)
ax3.set_ylabel('r-squared',fontsize=20)

输出:

图形输出

标签: pythonmatplotlib

解决方案


将月份添加到主要刻度格式化程序:

ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%m\n%Y"))

推荐阅读