{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# An example with a recorded interaction\n\nThis example has an associated playback file: ``_plot_dynamic-playback.json``\nso the ``mpl_playback`` sphinx-gallery scraper will be used to generate a gif\nto embed in the gallery. Contrast this with :doc:`/gallery/static`\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.widgets import Button, Slider\n\n\n# The parametrized function to be plotted\ndef f(t, amplitude, frequency):\n    return amplitude * np.sin(2 * np.pi * frequency * t)\n\n\nt = np.arange(0.0, 1.0, 0.001)\n\n# Define initial parameters\ninit_amplitude = 5\ninit_frequency = 3\n\n# Create the figure and the line that we will manipulate\nfig, ax = plt.subplots()\n(line,) = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2)\n\naxcolor = \"lightgoldenrodyellow\"\nax.margins(x=0)\n\n# adjust the main plot to make room for the sliders\nplt.subplots_adjust(left=0.25, bottom=0.25)\n\n# Make a horizontal slider to control the frequency.\naxfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)\nfreq_slider = Slider(\n    ax=axfreq,\n    label=\"Frequency\",\n    valmin=0.1,\n    valmax=30.0,\n    valinit=init_frequency,\n)\n\n# Make a vertically oriented slider to control the amplitude\naxamp = plt.axes([0.1, 0.25, 0.0225, 0.63], facecolor=axcolor)\namp_slider = Slider(\n    ax=axamp,\n    label=\"Amplitude\",\n    valmin=0.1,\n    valmax=10.0,\n    valinit=init_amplitude,\n    orientation=\"vertical\",\n)\n\n\n# The function to be called anytime a slider's value changes\ndef update(val):\n    line.set_ydata(f(t, amp_slider.val, freq_slider.val))\n    fig.canvas.draw_idle()\n\n\n# register the update function with each slider\nfreq_slider.on_changed(update)\namp_slider.on_changed(update)\n\n# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.\nresetax = plt.axes([0.8, 0.025, 0.1, 0.04])\nbutton = Button(resetax, \"Reset\", color=axcolor, hovercolor=\"0.975\")\n\n\ndef reset(event):\n    freq_slider.reset()\n    amp_slider.reset()\n\n\nbutton.on_clicked(reset)\n\nplt.show()"
      ]
    }
  ],
  "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.10.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}