
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/widgets/radio_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_radio_buttons.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_widgets_radio_buttons.py:


=============
Radio Buttons
=============

Using radio buttons to choose properties of your plot.

Radio buttons let you choose between multiple options in a visualization.
In this case, the buttons let the user choose one of the three different sine
waves to be shown in the plot.

Radio buttons may be styled using the *label_props* and *radio_props* parameters, which
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-79

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.widgets import RadioButtons

    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(8*np.pi*t)

    fig, ax = plt.subplot_mosaic(
        [
            ['main', 'freq'],
            ['main', 'color'],
            ['main', 'linestyle'],
        ],
        width_ratios=[5, 1],
        layout='constrained',
    )
    l, = ax['main'].plot(t, s0, lw=2, color='red')

    radio_background = 'lightgoldenrodyellow'

    ax['freq'].set_facecolor(radio_background)
    radio = RadioButtons(ax['freq'], ('1 Hz', '2 Hz', '4 Hz'),
                         label_props={'color': 'cmy', 'fontsize': [12, 14, 16]},
                         radio_props={'s': [16, 32, 64]})


    def hzfunc(label):
        hzdict = {'1 Hz': s0, '2 Hz': s1, '4 Hz': s2}
        ydata = hzdict[label]
        l.set_ydata(ydata)
        fig.canvas.draw()
    radio.on_clicked(hzfunc)

    ax['color'].set_facecolor(radio_background)
    radio2 = RadioButtons(
        ax['color'], ('red', 'blue', 'green'),
        label_props={'color': ['red', 'blue', 'green']},
        radio_props={
            'facecolor': ['red', 'blue', 'green'],
            'edgecolor': ['darkred', 'darkblue', 'darkgreen'],
        })


    def colorfunc(label):
        l.set_color(label)
        fig.canvas.draw()
    radio2.on_clicked(colorfunc)

    ax['linestyle'].set_facecolor(radio_background)
    radio3 = RadioButtons(ax['linestyle'], ('-', '--', '-.', ':'))


    def stylefunc(label):
        l.set_linestyle(label)
        fig.canvas.draw()
    radio3.on_clicked(stylefunc)

    plt.show()




.. image-sg:: /gallery/widgets/images/sphx_glr_radio_buttons_001.png
   :alt: radio buttons
   :srcset: /gallery/widgets/images/sphx_glr_radio_buttons_001.png, /gallery/widgets/images/sphx_glr_radio_buttons_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 80-86

.. admonition:: References

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

   - `matplotlib.widgets.RadioButtons`


.. _sphx_glr_download_gallery_widgets_radio_buttons.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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