{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Slider\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.widgets import Slider, Button, RadioButtons\n\nfig, ax = plt.subplots()\nplt.subplots_adjust(left=0.25, bottom=0.25)\nt = np.arange(0.0, 1.0, 0.001)\na0 = 5\nf0 = 3\ndelta_f = 5.0\ns = a0 * np.sin(2 * np.pi * f0 * t)\n(l,) = plt.plot(t, s, lw=2, color=\"red\")\nplt.axis([0, 1, -10, 10])\n\naxcolor = \"lightgoldenrodyellow\"\naxfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)\naxamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)\n\nsfreq = Slider(axfreq, \"Freq\", 0.1, 30.0, valinit=f0, valstep=delta_f)\nsamp = Slider(axamp, \"Amp\", 0.1, 10.0, valinit=a0)\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\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 sfreq.reset()\n samp.reset()\n\n\nbutton.on_clicked(reset)\n\nrax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)\nradio = RadioButtons(rax, (\"red\", \"blue\", \"green\"), active=0)\n\n\ndef colorfunc(label):\n l.set_color(label)\n fig.canvas.draw_idle()\n\n\nradio.on_clicked(colorfunc)\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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }