
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/widgets/check_buttons.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_widgets_check_buttons.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_widgets_check_buttons.py:


=============
Check buttons
=============

Turning visual elements on and off with check buttons.

This program shows the use of `.CheckButtons` which is similar to
check boxes. There are 3 different sine waves shown, and we can choose which
waves are displayed with the check buttons.

Check buttons may be styled using the *check_props*, *frame_props*, and *label_props*
parameters. The parameters each take a dictionary with keys of artist property names and
values of lists of settings with length matching the number of buttons.

.. GENERATED FROM PYTHON SOURCE LINES 16-56

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.widgets import CheckButtons

    t = np.arange(0.0, 2.0, 0.01)
    s0 = np.sin(2*np.pi*t)
    s1 = np.sin(4*np.pi*t)
    s2 = np.sin(6*np.pi*t)

    fig, ax = plt.subplots()
    l0, = ax.plot(t, s0, visible=False, lw=2, color='black', label='1 Hz')
    l1, = ax.plot(t, s1, lw=2, color='red', label='2 Hz')
    l2, = ax.plot(t, s2, lw=2, color='green', label='3 Hz')

    lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
    line_colors = [l.get_color() for l in lines_by_label.values()]

    # Make checkbuttons with all plotted lines with correct visibility
    rax = ax.inset_axes([0.0, 0.0, 0.12, 0.2])
    check = CheckButtons(
        ax=rax,
        labels=lines_by_label.keys(),
        actives=[l.get_visible() for l in lines_by_label.values()],
        label_props={'color': line_colors},
        frame_props={'edgecolor': line_colors},
        check_props={'facecolor': line_colors},
    )


    def callback(label):
        ln = lines_by_label[label]
        ln.set_visible(not ln.get_visible())
        ln.figure.canvas.draw_idle()

    check.on_clicked(callback)

    plt.show()




.. image-sg:: /gallery/widgets/images/sphx_glr_check_buttons_001.png
   :alt: check buttons
   :srcset: /gallery/widgets/images/sphx_glr_check_buttons_001.png, /gallery/widgets/images/sphx_glr_check_buttons_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 57-63

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.widgets.CheckButtons`


.. _sphx_glr_download_gallery_widgets_check_buttons.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: check_buttons.ipynb <check_buttons.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: check_buttons.py <check_buttons.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: check_buttons.zip <check_buttons.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
