I'm trying to draw an angled line on a chart with Matplotlib, but there are times when it just draws a vertical line, incorrectly. It appears to be a problem with the dates/times. It's like there's a minimum time required between points before it will plot correctly. Can anyone shed any light on this?
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()
CodePudding user response:
The reason why this happens is quite tricky to get without digging a bit deeper. When converted using date2num
, timestamps become decimal numbers like 19126.661111111112
.