7

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?

2
  • Hi Did you find a solution? Commented Apr 9, 2020 at 11:59
  • For use in tkinter, it worked when I destroyed the canvas widget and recreated the plot and the widget (but there is a brief flash due to that). Not sure how could it be done in default GUI. Commented Mar 9, 2022 at 22:09

1 Answer 1

0

Try this:

ani.frame_seq = ani.new_frame_seq() 
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work on my machine. You could try this method: html-version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.