亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在嘗試使用 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()

制作:

http://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()

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

查看完整回答
反對 回復 2023-05-16
  • 3 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號