.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\matplotlib\demo_buttons.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_matplotlib_demo_buttons.py: Buttons ========== .. GENERATED FROM PYTHON SOURCE LINES 5-46 .. image-sg:: /11_demos/python_packages/matplotlib/images/sphx_glr_demo_buttons_001.png :alt: demo buttons :srcset: /11_demos/python_packages/matplotlib/images/sphx_glr_demo_buttons_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Button freqs = np.arange(2, 20, 3) fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.2) t = np.arange(0.0, 1.0, 0.001) s = np.sin(2 * np.pi * freqs[0] * t) (l,) = plt.plot(t, s, lw=2) class Index: ind = 0 def next(self, event): self.ind += 1 i = self.ind % len(freqs) ydata = np.sin(2 * np.pi * freqs[i] * t) l.set_ydata(ydata) plt.draw() def prev(self, event): self.ind -= 1 i = self.ind % len(freqs) ydata = np.sin(2 * np.pi * freqs[i] * t) l.set_ydata(ydata) plt.draw() callback = Index() axprev = plt.axes([0.7, 0.05, 0.1, 0.075]) axnext = plt.axes([0.81, 0.05, 0.1, 0.075]) bnext = Button(axnext, "Next") bnext.on_clicked(callback.next) bprev = Button(axprev, "Previous") bprev.on_clicked(callback.prev) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 seconds) .. _sphx_glr_download_11_demos_python_packages_matplotlib_demo_buttons.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_buttons.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_buttons.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_buttons.zip `