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?
Try this: