.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/animation/pause_resume.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_animation_pause_resume.py: ================================= Pausing and Resuming 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 this will not appear in the static documentation. Please run this code on your machine to see the interactivity. You can copy and paste individual parts, or download the entire example using the link at the bottom of the page. .. GENERATED FROM PYTHON SOURCE LINES 19-57 .. image-sg:: /gallery/animation/images/sphx_glr_pause_resume_001.png :alt: Click to pause/resume the animation :srcset: /gallery/animation/images/sphx_glr_pause_resume_001.png, /gallery/animation/images/sphx_glr_pause_resume_001_2_0x.png 2.0x :class: sphx-glr-single-img .. code-block:: default import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np class PauseAnimation: 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 distribution self.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 = False fig.canvas.mpl_connect('button_press_event', self.toggle_pause) def toggle_pause(self, *args, **kwargs): if self.paused: self.animation.resume() else: self.animation.pause() self.paused = not self.paused def update(self, i): self.n0 += i / 100 % 5 self.p.set_ydata(self.n0 % 20) return (self.p,) pa = PauseAnimation() plt.show() .. _sphx_glr_download_gallery_animation_pause_resume.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pause_resume.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pause_resume.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_