首页 > 解决方案 > Matplotlib xaxis.set_major_formatter 不理解年份

问题描述

信息

我有以下绘制的 pd.dataframe

ax = dfPivot.plot()
plt.grid()
plt.grid(which='minor')
plt.show()

In [4]: dfPivot.index
Out[4]:
DatetimeIndex(['2020-05-26 12:00:33', '2020-05-26 12:02:13',
               '2020-05-26 12:03:53', '2020-05-26 12:05:33',
               '2020-05-26 12:07:13', '2020-05-26 12:08:53',
               '2020-05-26 12:10:33', '2020-05-26 12:12:13',
               '2020-05-26 12:13:53', '2020-05-26 12:15:33',
               ...
               '2020-05-29 09:08:53', '2020-05-29 09:10:33',
               '2020-05-29 09:12:13', '2020-05-29 09:13:53',
               '2020-05-29 09:15:33', '2020-05-29 09:17:13',
               '2020-05-29 09:18:53', '2020-05-29 09:20:33',
               '2020-05-29 09:22:13', '2020-05-29 09:23:53'],
              dtype='datetime64[ns]', name='DateTime', length=2499, freq=None)

它工作正常。

情节没有


问题

当我使用以下格式格式化 X 轴时遇到问题:

myFmt = mdates.DateFormatter("%d/%m/%y %H:%M:%S") # tried with "%x %X" or any other combination, failed.
ax.xaxis.set_major_formatter(myFmt)

蟒蛇掉了一个:

  rom_ordinalf
      dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC) ValueError: year 4354626 is out of range

当我使用 set_major_formatter() 时,我不明白哪个被处理不同。我认为这只是美学上的改变,但似乎更深入。

我认为 matplotlib 正在“尝试”在我的 pd.datetime64[ns] 上查找和美国类型日期并且失败但不知道如何继续。

谢谢

标签: pythonpandasmatplotlib

解决方案


推荐阅读