{
  "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\nFREQUENCIES = {'1 Hz': 1, '2 Hz': 2, '4 Hz': 4}\n\nt = np.arange(0.0, 2.0, 0.01)\n\n\ndef f(t, freq):\n    return np.sin(2 * np.pi * freq * t)\n\nfig, axd = plt.subplot_mosaic(\n    [\n        ['main', 'freq'],\n        ['main', 'color'],\n        ['main', 'linestyle'],\n    ],\n    width_ratios=[5, 1],\n    layout='constrained',\n)\n(line,) = axd['main'].plot(t, f(t, freq=1), lw=2, color='red')\naxd['main'].set(xlabel=\"Time (s)\", ylabel=\"Amplitude\", title=\"Sine Wave\")\n\nbackground_color = '0.95'\nedge_color = '0.8'\n\naxd['freq'].set_facecolor(background_color)\naxd['freq'].spines[:].set_color(edge_color)\naxd['freq'].set_title('Frequency')\nradio = RadioButtons(axd['freq'], labels=list(FREQUENCIES.keys()),\n                     label_props={'fontsize': [12, 14, 16]},\n                     radio_props={'s': [16, 32, 64]})\n\n\ndef update_frequency(label):\n    ydata = f(t, freq=FREQUENCIES[label])\n    line.set_ydata(ydata)\n    fig.canvas.draw()\nradio.on_clicked(update_frequency)\n\n\naxd['color'].set_facecolor(background_color)\naxd['color'].spines[:].set_color(edge_color)\naxd['color'].set_title('Color')\nradio2 = RadioButtons(\n    axd['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 update_color(label):\n    line.set_color(label)\n    fig.canvas.draw()\nradio2.on_clicked(update_color)\n\n\naxd['linestyle'].set_facecolor(background_color)\naxd['linestyle'].spines[:].set_color(edge_color)\naxd['linestyle'].set_title('Linestyle')\nradio3 = RadioButtons(axd['linestyle'], ('solid', 'dashed', 'dashdot', 'dotted'))\n\n\ndef update_linestyle(label):\n    line.set_linestyle(label)\n    fig.canvas.draw()\nradio3.on_clicked(update_linestyle)\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.14.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}