为了账号安全,请及时绑定邮箱和手机立即绑定

为什么在尝试使用 matplotlib 绘制它们时时间是 00:00?

为什么在尝试使用 matplotlib 绘制它们时时间是 00:00?

心有法竹 2023-05-16 15:01:45
我有一个列表datetime,我正在尝试绘制一个数字,其中时间在 y 轴上,日期在 x 轴上。以下是我到目前为止所得到的:import datetime as dtimport matplotlib.pyplot as pltfrom matplotlib.dates import DateFormatterdatetimes_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:47fig, 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()上面的代码导致 -如您所见,日期是正确的,但所有时间都是00:00。有什么问题?!还有一些奇怪的事情:在列表中datetimes,当所有日期都相同且时间不同时,一切正常!
查看完整描述

3 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

这是因为DateFormatter从日期的序数值中删除了浮点数:


~/.local/bin/anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz)

    281         tz = _get_rc_timezone()

    282 

--> 283     ix, remainder = divmod(x, 1)

    284     ix = int(ix)

    285     if ix < 1:

您必须使用ax.set_yticks或最好创建自己的刻度/刻度标签ax.set_yticklabels


查看完整回答
反对 回复 2023-05-16
?
慕村9548890

TA贡献1884条经验 获得超4个赞

您可以在列表理解中对时间进行切片:


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]

times = sorted([d[11:16] for d in datetimes_str])


fig, ax = plt.subplots()

ax.plot(datetimes, times)

ax.xaxis.set_major_formatter(DateFormatter('%Y/%m/%d'))

fig.autofmt_xdate()

plt.show()

制作:

//img1.sycdn.imooc.com//64632a840001052805750401.jpg

查看完整回答
反对 回复 2023-05-16
?
繁星coding

TA贡献1797条经验 获得超4个赞

发生这种情况是因为 x 轴和 y 轴唯一不同的是表示日期时间的格式,而不是日期时间本身的值,这就是为什么你总是会得到一条对角线。


所以只显示 00:00,因为 y 轴的值变化太大而无法精确表示小时数。


为了更改此设置,您需要通过删除日期信息并仅保留时间来更改 y 轴的值。这是一个应该可以解决问题的代码片段:


import datetime as dt

import matplotlib.pyplot as plt

from matplotlib.dates import DateFormatter, DayLocator, HourLocator


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]

times = [d.replace(day=1, month=1, year=2000) for d in datetimes]


# 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.yaxis.set_major_formatter(DateFormatter('%H:%M'))

ax.xaxis.set_major_formatter(DateFormatter('%Y/%m/%d'))


ax.plot(datetimes, times)


fig.autofmt_xdate()


plt.show()

//img1.sycdn.imooc.com//64632a970001631203820252.jpg

查看完整回答
反对 回复 2023-05-16
  • 3 回答
  • 0 关注
  • 169 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信