如何在不重新运行脚本的情况下重置Matplotlib动画 [英] How to reset matplotlib animation without re-running script

查看:117
本文介绍了如何在不重新运行脚本的情况下重置Matplotlib动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib的FuncAnimation函数为大型数据集的一部分设置动画:

I'm using matplotlib's FuncAnimation function to animate a segment of a large dataset:

fig = plt.figure(figsize=(15, 11.5))
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False,
                     xlim=(x1,x2), ylim=(y1, y2))

data,=ax.plot([],[],'o')

def init():
    data.set_data([],[])
    return data


def animate(t):
    global x_pos,y_pos
    data.set_data(x_pos[t],y_pos[t])
    return data

ani=animation.FuncAnimation(fig,animate,frames=duration,interval=20,
                            init_func=init,blit=True)


plt.show()

当我从头开始运行代码时,此方法有效大。但是,由于这涉及到加载和预处理大型数据集,因此需要几分钟的时间,因此,我希望仅运行代码的一部分进行测试和动画处理。

When I run my code from scratch this works great. However, since that involves loading up and preprocessing a large dataset it takes a few minutes, so I'd like to be able to run just a segment of my code to test and animate.

但是,当我关闭动画并尝试再次运行它时,我只剩下一个空白图形-没有绘制点,并且animate()函数永远不会

When I close my animation and try running it again, however, I'm left with only a blank figure - no points are plotted and the animate() function is never called (I tested this with a print statement).

我尝试清除图表和图形:

I tried clearing the plot and the figure:

plt.clf()
fig.close()
plt.clear(figure)

并尝试使用不同的数字,但结果相同。

and trying a different figure number, but the results are the same.

如何清除动画,以便可以在不重新运行我的整个脚本的情况下再次运行它?

How can I clear the animation so that I can run it again without re-running my entire script?

推荐答案

尝试一下:

ani.frame_seq = ani.new_frame_seq() 

这篇关于如何在不重新运行脚本的情况下重置Matplotlib动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆