Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

The reason for this is that the range of time series data handled by matplotlib is large, see this for matplotlib dates that appear to be too small to draw the data you are dealing with. So I draw the numbers on the x-axis as variable i, and the current time in string format as a list. For the sake of sample creation, my code is set to milliseconds. In your case it will be ts.append(dt.datetime.now().strftime('%H')). Is this answer what you intend?

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.dates as mdates
import random
import datetime as dt
from IPython.display import HTML
import decimal
import numpy as np
from matplotlib.animation import PillowWriter

xs = []
ys = []
ts = []
random.seed(2021, 2)
decimal.getcontext().prec = 3
gateStart = random.randint(0, 100)
waiting = False
returning = False
paused = False
currentTime = dt.datetime.today()

fig = plt.figure(figsize=(14, 6))
ax = plt.axes(xlim=(0,100),ylim=(0, 100))
line, = ax.plot([], [], 'r-', lw=3)
ax.set_ylabel('Shearer Position')
ax.set_title('Longwall Data')

# This function is called periodically from FuncAnimation
def animate(i, xs, ys):
    global gateStart, waiting, returning, paused

    if gateStart == 100:
        returning = True
    elif gateStart == 0:
        returning = False

    if returning:
        gateStart = round(gateStart - 1, 1)
    else:
        gateStart = round(gateStart + 1, 1)

#     print(dt.datetime.now())
    # Add x and y to lists
    xs.append(i)
    ys.append(gateStart)
    ts.append(dt.datetime.now().strftime('%f')) # Microsecond(6)

    # Draw x and y lists
    line.set_data(xs, ys)
    # Format plot
    ax.set_xticks(np.arange(len(ts)))
    ax.set_xticklabels(ts, rotation=90)

ani = FuncAnimation(fig, animate, fargs=(xs, ys), interval=200, repeat=False)
# ani.save('realtime_plot_anim.gif', writer='pillow')
plt.show()
# jupyter lab 
# plt.close()
# HTML(ani.to_html5_video())

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...