简体   繁体   中英

Line on animated matplotlib graph not showing up

I'm not sure why a graph line isn't showing on my widget.

The x-axis is moving with time as it should, and when I print the y-values that should be plotted (Temp), new values come up as expected. However, no line/points show up on the plot.

I've had a similar issue with a plot in the past, which was solved by changing the style of the points plotted (ie using 'r*' to make the points visible red stars). I'm not sure how to implement the same sort of thing in this code.

import matplotlib.pyplot as plt
import numpy
import datetime
import matplotlib.animation as animation

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    Time = []
    Temp = []
    x = datetime.datetime.now()
    y = numpy.random.randint(48,52)
    Time.append(x)
    Temp.append(int(y))    

    ax1.plot(Time,Temp)
    print(Temp)

ani = animation.FuncAnimation(fig,animate, interval=1000)
plt.show()

您可以添加marker并显示点:

ax1.plot(Time,Temp, marker="s")

just put Time and Temp out of function animate

Time = []
Temp = []
def animate(i):
    x = datetime.datetime.now()
    y = numpy.random.randint(48,52)
    Time.append(x)
    Temp.append(int(y))    
    ax1.plot(Time,Temp)
    print(Temp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM