为什么尝试使用matplotlib绘制时间为00:00?

问题描述

我有一个datetime列表,我试图绘制一个图,其中时间在y轴上,日期在x轴上。以下是到目前为止我得到的:

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

datetimes_str = ['2020-08-03T03:46:18.000Z','2020-08-01T01:14:31.000Z','2020-07-27T22:45:11.000Z','2020-07-21T20:00:42.000Z','2020-07-20T00:37:17.000Z','2020-07-16T00:40:47.000Z']

datetimes = [dt.datetime.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ") for d in datetimes_str]

# The above list contains datetimes:
# 2020-08-03 03:46:18
# 2020-08-01 01:14:31
# 2020-07-27 22:45:11
# 2020-07-21 20:00:42
# 2020-07-20 00:37:17
# 2020-07-16 00:40:47

fig,ax = plt.subplots()

ax.plot(datetimes,datetimes)

ax.yaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.xaxis.set_major_formatter(DateFormatter('%Y/%m/%d'))

fig.autofmt_xdate()

plt.show()

上面的代码导致-

enter image description here

您看到日期是正确的,但所有时间均为00:00。怎么了?!

还有一个奇怪的事情:在列表datetimes中,当所有日期都相等且时间不同时,一切正常!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)