{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Radio Buttons\n\nUsing radio buttons to choose properties of your plot.\n\nRadio buttons let you choose between multiple options in a visualization.\nIn this case, the buttons let the user choose one of the three different sine\nwaves to be shown in the plot.\n\nRadio buttons may be styled using the *label_props* and *radio_props* parameters, which\neach take a dictionary with keys of artist property names and values of lists of\nsettings with length matching the number of buttons.\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 RadioButtons\n\nt = np.arange(0.0, 2.0, 0.01)\ns0 = np.sin(2*np.pi*t)\ns1 = np.sin(4*np.pi*t)\ns2 = np.sin(8*np.pi*t)\n\nfig, ax = plt.subplot_mosaic(\n [\n ['main', 'freq'],\n ['main', 'color'],\n ['main', 'linestyle'],\n ],\n width_ratios=[5, 1],\n layout='constrained',\n)\nl, = ax['main'].plot(t, s0, lw=2, color='red')\n\nradio_background = 'lightgoldenrodyellow'\n\nax['freq'].set_facecolor(radio_background)\nradio = RadioButtons(ax['freq'], ('1 Hz', '2 Hz', '4 Hz'),\n label_props={'color': 'cmy', 'fontsize': [12, 14, 16]},\n radio_props={'s': [16, 32, 64]})\n\n\ndef hzfunc(label):\n hzdict = {'1 Hz': s0, '2 Hz': s1, '4 Hz': s2}\n ydata = hzdict[label]\n l.set_ydata(ydata)\n fig.canvas.draw()\nradio.on_clicked(hzfunc)\n\nax['color'].set_facecolor(radio_background)\nradio2 = RadioButtons(\n ax['color'], ('red', 'blue', 'green'),\n label_props={'color': ['red', 'blue', 'green']},\n radio_props={\n 'facecolor': ['red', 'blue', 'green'],\n 'edgecolor': ['darkred', 'darkblue', 'darkgreen'],\n })\n\n\ndef colorfunc(label):\n l.set_color(label)\n fig.canvas.draw()\nradio2.on_clicked(colorfunc)\n\nax['linestyle'].set_facecolor(radio_background)\nradio3 = RadioButtons(ax['linestyle'], ('-', '--', '-.', ':'))\n\n\ndef stylefunc(label):\n l.set_linestyle(label)\n fig.canvas.draw()\nradio3.on_clicked(stylefunc)\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.RadioButtons`\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }