我正在嘗試使用 python 在同一個圖形上繪制多個圖。該圖應該是線性的,x 坐標代表一天中的時間,y 坐標與值匹配。每個情節匹配不同的日期。
資料存盤在字典中。鍵代表日期,值包含 2 個串列:第一個匹配 x 坐標,第二個匹配 y 坐標。例如:
dict_data = {"4 April": [[datetime(1900, 1, 1, 22, 59), datetime(1900, 1, 1, 23, 1), datetime(1900, 1, 1, 23, 8), datetime(1900, 1, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime(1900, 1, 1, 8, 10), datetime(1900, 1, 1, 9, 40), datetime(1900, 1, 1, 11, 8), datetime(1900, 1, 1, 11, 10)], [120, 20, 10, 0]]}
我在 Python 中使用 Matplotlib 發現堆疊溢位繪圖時間的帖子。這沒有幫助,因為他們創建的圖表上的 x 軸是“datetime”型別,而我使用“datetime.time”datetime”。(我不希望 x 軸顯示日期)。還有,他們創建的圖表是散點圖,而我需要它是線性的。
這是我嘗試過的:
def multiple_plots(dict_data):
"""
Method to plot multiple times in one figure.
It receives a dictionary with the representation of the data in the csv file.
Every key in the dictionary represent a different date that will have its own plot ont the graph.
"""
for date, coordinates in dict_data.items():
time_coordinates = coordinates[0]
# converting the x coordinates in the type datetime.time to int
x_coordinates = matplotlib.dates.date2num(time_coordinates)
val_coordinates = coordinates[1]
plt.plot(list(map(int, x_coordinates)), list(map(int, val_coordinates)), label=date)
plt.legend(loc='best')
plt.show()
uj5u.com熱心網友回復:
這應該可以毫不費力地作業:
import matplotlib.pyplot as plt
import datetime
dict_data = {"4 April": [[datetime.datetime(1900, 1, 1, 22, 59), datetime.datetime(1900, 1, 1, 23, 1), datetime.datetime(1900, 1, 1, 23, 8), datetime.datetime(1900, 1, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime.datetime(1900, 1, 1, 8, 10), datetime.datetime(1900, 1, 1, 9, 40), datetime.datetime(1900, 1, 1, 11, 8), datetime.datetime(1900, 1, 1, 11, 10)], [120, 20, 10, 0]]}
fig, ax = plt.subplots()
for k in dict_data:
ax.plot(dict_data[k][0], dict_data[k][1])
plt.show()

顯然這看起來有點局促,但如果你使用簡潔的轉換器:
plt.rcParams['date.converter'] = 'concise'
然后你得到
uj5u.com熱心網友回復:
基于有問題的編輯進行編輯
代碼,使用plot_date和線型'-'。
import matplotlib
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
dict_data = {"4 April": [[np.datetime64("2022-07-01T22:59:00"), np.datetime64("2022-07-01T23:01:00"), np.datetime64("2022-07-01T23:08:00"), np.datetime64("2022-07-01T23:50:00")], [405, 320, 300, 360]], "5 April": [[np.datetime64("2022-07-01T08:10:00"), np.datetime64("2022-07-01T09:40:00"), np.datetime64("2022-07-01T11:08:00"), np.datetime64("2022-07-01T11:10:00")], [120, 20, 10, 0]]}
# dict_data = {"4 April": [[datetime(2022, 7, 1, 22, 59), datetime(2022, 7, 1, 23, 1), datetime(2022, 7, 1, 23, 8), datetime(2022, 7, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime(2022, 7, 1, 8, 10), datetime(2022, 7, 1, 9, 40), datetime(2022, 7, 1, 11, 8), datetime(2022, 7, 1, 11, 10)], [120, 20, 10, 0]]}
def multiple_plots(dict_data):
"""
Method to plot multiple times in one figure.
It receives a dictionary with the representation of the data in the csv file.
Every key in the dictionary represent a different date that will have its own plot ont the graph.
"""
for date, coordinates in dict_data.items():
time_coordinates = coordinates[0]
# converting the x coordinates in the type np.datetime64 t2022-07-01To int
x_coordinates = matplotlib.dates.date2num(time_coordinates)
val_coordinates = coordinates[1]
plt.plot_date(list(map(int, x_coordinates)), list(map(int, val_coordinates)), '-', label=date)
plt.legend(loc='best')
plt.show()
multiple_plots(dict_data)
最終輸出:

date2num除了時間之外,該功能還需要年、月和日。因此,兩個簡單的修復是:
- 發送年、月和時間
dict_data = {"4 April": [[datetime(2022, 7, 1, 22, 59), datetime(2022, 7, 1, 23, 1), datetime(2022, 7, 1, 23, 8), datetime(2022, 7, 1, 23, 50)], [405, 320, 300, 360]], "5 April": [[datetime(2022, 7, 1, 8, 10), datetime(2022, 7, 1, 9, 40), datetime(2022, 7, 1, 11, 8), datetime(2022, 7, 1, 11, 10)], [120, 20, 10, 0]]}
- 發送
np.datetime64型別
dict_data = {"4 April": [[np.datetime64("2022-07-01T22:59:00"), np.datetime64("2022-07-01T23:01:00"), np.datetime64("2022-07-01T23:08:00"), np.datetime64("2022-07-01T23:50:00")], [405, 320, 300, 360]], "5 April": [[np.datetime64("2022-07-01T08:10:00"), np.datetime64("2022-07-01T09:40:00"), np.datetime64("2022-07-01T11:08:00"), np.datetime64("2022-07-01T11:10:00")], [120, 20, 10, 0]]}
他們都作業。最后的情節是:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/406541.html
標籤:
