带有时间戳的Matplotlib散点图动画

2024-05-23 14:11:46 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,提前感谢您的任何提示和建议。我试图创建一个动画散点图,它根据我读入python的模型结果改变点的颜色。如果我可以在动画的同时显示一个更新的时间戳,那么模型的结果会更容易解释。在

我试图在这篇文章中加入答案: Matplotlib animating multiple lines and text,但我认为我使用的是分散数据集而不是直线,这会使数据返回的方式复杂化,而且我不确定如何纠正这个问题。在

这段代码交替地闪烁时间步和散射动画,这会造成分散注意力和无用的视觉效果。在

 # load result
testModel.addResult(testDye,type='dye')
xs = testModel.grid['x']
ys = testModel.grid['y']
zs = testModel.grid['z']

# graphing
fig = plt.figure()
ax = fig.add_subplot(111)
gcf = plt.gcf()
scat = ax.scatter(xs,ys)
timetext = gcf.text(0.2, 0.2, '', size = 14)

def animate(i):
    print i
    actDye = testModel.getResult(type='dye',layer=1,tIndex=i)
    scat.set_array((actDye/1000)*100) #update colors of points 
    timetext.set_text(i)
    return scat,

def init():
    actDye = testModel.getResult(type='dye',layer=1,tIndex=0)
    scat.set_array((actDye/1000)*100) #update colors of points
    return scat,

ani = animation.FuncAnimation(fig,animate,np.arange(0,200),init_func=init, interval=500, blit=True)

plt.show()

我认为返回timetextscat,可以解决问题(就像对另一张海报一样),但是我不能正确地理解语法。切换到这段代码会给我一个错误,即“PathCollection”对象不可读取。在

^{pr2}$

我该怎么做呢?谢谢!在


Tags: text模型inittypefig动画pltgcf