{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Check buttons\n\nTurning visual elements on and off with check buttons.\n\nThis program shows the use of `.CheckButtons` which is similar to\ncheck boxes. There are 3 different sine waves shown, and we can choose which\nwaves are displayed with the check buttons.\n\nCheck buttons may be styled using the *check_props*, *frame_props*, and *label_props*\nparameters. The parameters each take a dictionary with keys of artist property names and\nvalues of lists of settings 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 CheckButtons\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(6*np.pi*t)\n\nfig, ax = plt.subplots()\nl0, = ax.plot(t, s0, visible=False, lw=2, color='black', label='1 Hz')\nl1, = ax.plot(t, s1, lw=2, color='red', label='2 Hz')\nl2, = ax.plot(t, s2, lw=2, color='green', label='3 Hz')\n\nlines_by_label = {l.get_label(): l for l in [l0, l1, l2]}\nline_colors = [l.get_color() for l in lines_by_label.values()]\n\n# Make checkbuttons with all plotted lines with correct visibility\nrax = ax.inset_axes([0.0, 0.0, 0.12, 0.2])\ncheck = CheckButtons(\n ax=rax,\n labels=lines_by_label.keys(),\n actives=[l.get_visible() for l in lines_by_label.values()],\n label_props={'color': line_colors},\n frame_props={'edgecolor': line_colors},\n check_props={'facecolor': line_colors},\n)\n\n\ndef callback(label):\n ln = lines_by_label[label]\n ln.set_visible(not ln.get_visible())\n ln.figure.canvas.draw_idle()\n\ncheck.on_clicked(callback)\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.CheckButtons`\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 }