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

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

.. _sphx_glr_gallery_widgets_slider_snap_demo.py:


===============================
Snap sliders to discrete values
===============================

You can snap slider values to discrete values using the ``valstep`` argument.

In this example the Freq slider is constrained to be multiples of pi, and the
Amp slider uses an array as the ``valstep`` argument to more densely sample
the first part of its range.

See :doc:`/gallery/widgets/slider_demo` for an example of using
a ``Slider`` to control a single float.

See :doc:`/gallery/widgets/range_slider` for an example of using
a ``RangeSlider`` to define a range of values.

.. GENERATED FROM PYTHON SOURCE LINES 18-75

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.widgets import Button, Slider

    t = np.arange(0.0, 1.0, 0.001)
    a0 = 5
    f0 = 3
    s = a0 * np.sin(2 * np.pi * f0 * t)

    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.25)
    l, = ax.plot(t, s, lw=2)

    ax_freq = fig.add_axes([0.25, 0.1, 0.65, 0.03])
    ax_amp = fig.add_axes([0.25, 0.15, 0.65, 0.03])

    # define the values to use for snapping
    allowed_amplitudes = np.concatenate([np.linspace(.1, 5, 100), [6, 7, 8, 9]])

    # create the sliders
    samp = Slider(
        ax_amp, "Amp", 0.1, 9.0,
        valinit=a0, valstep=allowed_amplitudes,
        color="green"
    )

    sfreq = Slider(
        ax_freq, "Freq", 0, 10*np.pi,
        valinit=2*np.pi, valstep=np.pi,
        initcolor='none'  # Remove the line marking the valinit position.
    )


    def update(val):
        amp = samp.val
        freq = sfreq.val
        l.set_ydata(amp*np.sin(2*np.pi*freq*t))
        fig.canvas.draw_idle()


    sfreq.on_changed(update)
    samp.on_changed(update)

    ax_reset = fig.add_axes([0.8, 0.025, 0.1, 0.04])
    button = Button(ax_reset, 'Reset', hovercolor='0.975')


    def reset(event):
        sfreq.reset()
        samp.reset()
    button.on_clicked(reset)


    plt.show()




.. image-sg:: /gallery/widgets/images/sphx_glr_slider_snap_demo_001.png
   :alt: slider snap demo
   :srcset: /gallery/widgets/images/sphx_glr_slider_snap_demo_001.png, /gallery/widgets/images/sphx_glr_slider_snap_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 76-83

.. admonition:: References

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

   - `matplotlib.widgets.Slider`
   - `matplotlib.widgets.Button`


.. _sphx_glr_download_gallery_widgets_slider_snap_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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