【问题标题】:How to convert datetime index of matplotlib to month如何将matplotlib的日期时间索引转换为月份
【发布时间】:2020-10-05 22:20:26
【问题描述】:

我在 matplotlib notebook 中制作了这个图表。我想将日期时间转换为月份,因为它们看起来像这样混乱。怎么办?

【问题讨论】:

标签: python python-3.x datetime matplotlib


【解决方案1】:

最简单的方法可能是使用datetime 模块:

import datetime
x_values = [d.strftime("%b %y") for d in dates]
plt.plot(x_values, y_values)

哪个输出Jan 15 而不是2015-01。如果要绘制完整的年份和月份名称,可以使用"%B %Y"

【讨论】:

  • 这将为每个数据值提供一个字符串并打勾,这几乎肯定不是我们想要的。
  • 你能详细解释一下@SeamAhmed 的问题吗?
猜你喜欢
  • 2017-08-27
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
相关资源
最近更新 更多