{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Snap sliders to discrete values\n\nYou can snap slider values to discrete values using the ``valstep`` argument.\n\nIn this example the Freq slider is constrained to be multiples of pi, and the\nAmp slider uses an array as the ``valstep`` argument to more densely sample\nthe first part of its range.\n\nSee :doc:`/gallery/widgets/slider_demo` for an example of using\na ``Slider`` to control a single float.\n\nSee :doc:`/gallery/widgets/range_slider` for an example of using\na ``RangeSlider`` to define a range of values.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.widgets import Button, Slider\n\nt = np.arange(0.0, 1.0, 0.001)\na0 = 5\nf0 = 3\ns = a0 * np.sin(2 * np.pi * f0 * t)\n\nfig, ax = plt.subplots()\nfig.subplots_adjust(bottom=0.25)\nl, = ax.plot(t, s, lw=2)\n\nax_freq = fig.add_axes((0.25, 0.1, 0.65, 0.03))\nax_amp = fig.add_axes((0.25, 0.15, 0.65, 0.03))\n\n# define the values to use for snapping\nallowed_amplitudes = np.concatenate([np.linspace(.1, 5, 100), [6, 7, 8, 9]])\n\n# create the sliders\nsamp = Slider(\n    ax_amp, \"Amp\", 0.1, 9.0,\n    valinit=a0, valstep=allowed_amplitudes,\n    color=\"green\"\n)\n\nsfreq = Slider(\n    ax_freq, \"Freq\", 0, 10*np.pi,\n    valinit=2*np.pi, valstep=np.pi,\n    initcolor='none'  # Remove the line marking the valinit position.\n)\n\n\ndef update(val):\n    amp = samp.val\n    freq = sfreq.val\n    l.set_ydata(amp*np.sin(2*np.pi*freq*t))\n    fig.canvas.draw_idle()\n\n\nsfreq.on_changed(update)\nsamp.on_changed(update)\n\nax_reset = fig.add_axes((0.8, 0.025, 0.1, 0.04))\nbutton = Button(ax_reset, 'Reset', hovercolor='0.975')\n\n\ndef reset(event):\n    sfreq.reset()\n    samp.reset()\nbutton.on_clicked(reset)\n\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.widgets.Slider`\n   - `matplotlib.widgets.Button`\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}