python - 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)
输出:
解决方案
将月份添加到主要刻度格式化程序:
ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%m\n%Y"))
推荐阅读
- c++ - 绘制 QTableView 的背景(使用自定义 QStyledItemDelegate)
- java - 如何使用 Eclipse 环境编译 Java 11 程序?
- ruby-on-rails - Rails Active Storage - 如何将本地文件迁移到 s3 存储桶
- c# - 需要 C# LINQ 列表聚合表达式的建议
- javascript - Lint]:2 处的参数无效:在定义描述时无法从 bool 转换为 Function.prototype 错误,并且它在量角器上阻塞
- excel - 选择组合值时未加载图片
- ios - 是否可以自定义 iOS 密码自动填充布局(本机)
- python - 用 Python-OpenCV 手写的顺序数字识别和提取
- java - 未排序数组中的二进制搜索
- reactjs - 将不同的参数传递给 React useState