Note
Go to the endto download the full example code.
Pause and resume an animation#
This example showcases:
using the Animation.pause() method to pause an animation.
using the Animation.resume() method to resume an animation.
Note
This example exercises the interactive capabilities of Matplotlib, and thiswill not appear in the static documentation. Please run this code on yourmachine to see the interactivity.
You can copy and paste individual parts, or download the entire exampleusing the link at the bottom of the page.
Output generated viamatplotlib.animation.Animation.to_jshtml.

importmatplotlib.pyplotaspltimportnumpyasnpimportmatplotlib.animationasanimationclassPauseAnimation:def__init__(self):fig,ax=plt.subplots()ax.set_title('Click to pause/resume the animation')x=np.linspace(-0.1,0.1,1000)# Start with a normal distributionself.n0=(1.0/((4*np.pi*2e-4*0.1)**0.5)*np.exp(-x**2/(4*2e-4*0.1)))self.p,=ax.plot(x,self.n0)self.animation=animation.FuncAnimation(fig,self.update,frames=200,interval=50,blit=True)self.paused=Falsefig.canvas.mpl_connect('button_press_event',self.toggle_pause)deftoggle_pause(self,*args,**kwargs):ifself.paused:self.animation.resume()else:self.animation.pause()self.paused=notself.pauseddefupdate(self,i):self.n0+=i/100%5self.p.set_ydata(self.n0%20)return(self.p,)pa=PauseAnimation()plt.show()