我正在嘗試使用 Matplotlib 在圖表上繪制一條斜線,但有時它只是錯誤地繪制了一條垂直線。日期/時間似乎有問題。就像在正確繪制點之間需要最短時間一樣。任何人都可以對此有所了解嗎?
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
time = 1652536320 # Start time
unix_time = [time]
for i in range(4):
time = 16525
# There must be 16526 seconds (4.5 hours) between unix timestamps for the axline to plot correctly.
# Anything less incorrectly produces a vertical line.
unix_time.append(time)
non_unix_time = []
for t in unix_time:
dt = datetime.datetime.fromtimestamp(t)
converted = mdates.date2num(dt)
non_unix_time.append(converted)
print(unix_time)
print(non_unix_time)
fig = plt.figure()
ax3 = plt.subplot2grid((1, 1), (0, 0))
x = non_unix_time
y = [29675, 29813, 29840, 29761, 29746]
ax3.axline((non_unix_time[0], 29600), (non_unix_time[1], 29800))
ax3.plot(x, y)
plt.show()
uj5u.com熱心網友回復:
發生這種情況的原因很難在不深入挖掘的情況下得到。使用 轉換時date2num
,時間戳變成十進制數字,如19126.661111111112
.
繪制線條時,使用該函式matplotlib
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475962.html
標籤:Python matplotlib 图表 线