【问题标题】:How to reset matplotlib animation without re-running script如何在不重新运行脚本的情况下重置 matplotlib 动画
【发布时间】:2016-07-26 23:23:51
【问题描述】:

我正在使用 matplotlib 的 FuncAnimation 函数对大型数据集的一部分进行动画处理:

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()

当我从头开始运行我的代码时,效果很好。但是,由于这涉及加载和预处理大型数据集,因此需要几分钟,所以我希望能够只运行一段代码来测试和制作动画。

但是,当我关闭动画并尝试再次运行它时,我只剩下一个空白图形 - 没有绘制任何点,并且从未调用 animate() 函数(我使用 print 语句对此进行了测试)。

我尝试清除情节和人物:

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

并尝试不同的数字,但结果是一样的。

如何清除动画,以便无需重新运行整个脚本即可再次运行它?

【问题讨论】:

  • 嗨,您找到解决方案了吗?
  • 为了在 tkinter 中使用,它在我销毁画布小部件并重新创建绘图和小部件时起作用(但因此有短暂的闪光)。不确定如何在默认 GUI 中完成。

标签: python animation matplotlib figure


【解决方案1】:

试试这个:

ani.frame_seq = ani.new_frame_seq() 

【讨论】:

  • 这在我的机器上不起作用。你可以试试这个方法:html-version
猜你喜欢
  • 2013-12-12
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 2021-11-17
相关资源
最近更新 更多