嗨,我对matplotlib的实时绘图有一些问题。X轴和Y轴上的随机数。随机数是一个静态数,然后乘以一个随机数
import matplotlib.pyplot as plt
import datetime
import numpy as np
import time
def GetRandomInt(Data):
timerCount=0
x=[]
y=[]
while timerCount < 5000:
NewNumber = Data * np.random.randomint(5)
x.append(datetime.datetime.now())
y.append(NewNumber)
plt.plot(x,y)
plt.show()
time.sleep(10)
a = 10
GetRandomInt(a)
这似乎使python崩溃,因为它无法处理更新-我可以添加一个延迟,但想知道代码是否在做正确的事情?我已经清理了代码来做和我的代码相同的功能,所以我们的想法是我们有一些静态数据,然后我们想每隔5秒更新一些数据,然后绘制更新图。谢谢!在
要绘制一组连续的随机线图,需要在matplotlib中使用动画:
这里的想法是您有一个包含}值的图。其中
x
和{x
只是一个范围,例如0到5。然后调用animation.FuncAnimation()
,它告诉matplotlib每隔1000ms
调用你的animate()
函数,让你提供新的y
值。在您可以通过修改
interval
参数来任意加快速度。在一种可能的方法,如果您想绘制随时间变化的值,可以使用
^{pr2}$deque()
来保存y
值,然后使用x
轴来保存seconds ago
:给你:
您可以实例化GetRandomInt,它实例化PlotData,后者实例化GetRandomInt,后者实例化PlotData,后者。。。等等,这就是你问题的根源。在
相关问题 更多 >
编程相关推荐