I'm simulating real-time data by plotting values against system time every interval using FuncAnimation. I want to plot the y-axis (positional data, essentially a saw-tooth) against the current time of the animation interval over a range from the system time to 12 hours from then (i.e. 5pm- 5am). When I set this limit, no line is being drawn on the graph. What am I doing wrong?
fig, ax = plt.subplots(figsize=(10, 6))
xs = []
ys = []
random.seed(None, 2)
getcontext().prec = 3
gateStart = random.randint(0, 100)
waiting = False
returning = False
paused = False
currentTime = dt.datetime.today()
# This function is called periodically from FuncAnimation
def animate(i, xs, ys):
global gateStart, waiting, returning, paused
print(gateStart)
if gateStart == 100:
returning = True
elif gateStart == 0:
returning = False
if returning:
gateStart = round(gateStart - 0.1, 1)
else:
gateStart = round(gateStart + 0.1, 1)
# Add x and y to lists
xs.append(dt.datetime.now())
ys.append(gateStart)
# Draw x and y lists
ax.clear()
ax.plot(xs, ys)
# Format plot
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
endTime = currentTime + datetime.timedelta(hours=12)
plt.xlim([currentTime, endTime])
fig.autofmt_xdate()
plt.subplots_adjust(bottom=0.30)
plt.title('Longwall Data')
plt.ylabel('Shearer Position')
plt.ylim(0, 100)
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1)
plt.show()
question from:https://stackoverflow.com/questions/65662024/no-plot-being-drawn-when-using-x-axis-date-range