How to reset matplotlib animation without re-runni

2019-06-24 10:20发布

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.

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?

1条回答
萌系小妹纸
2楼-- · 2019-06-24 10:41

Try this:

ani.frame_seq = ani.new_frame_seq() 
查看更多
登录 后发表回答