Note
Go to the endto download the full example code.
Check buttons#
Turning visual elements on and off with check buttons.
This program shows the use ofCheckButtons
which is similar tocheck boxes. There are 3 different sine waves shown, and we can choose whichwaves are displayed with the check buttons.
Check buttons may be styled using thecheck_props,frame_props, andlabel_propsparameters. The parameters each take a dictionary with keys of artist property names andvalues of lists of settings with length matching the number of buttons.
importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.widgetsimportCheckButtonst=np.arange(0.0,2.0,0.01)s0=np.sin(2*np.pi*t)s1=np.sin(4*np.pi*t)s2=np.sin(6*np.pi*t)fig,ax=plt.subplots()l0,=ax.plot(t,s0,visible=False,lw=2,color='black',label='1 Hz')l1,=ax.plot(t,s1,lw=2,color='red',label='2 Hz')l2,=ax.plot(t,s2,lw=2,color='blue',label='3 Hz')lines_by_label={l.get_label():lforlin[l0,l1,l2]}line_colors=[l.get_color()forlinlines_by_label.values()]# Make checkbuttons with all plotted lines with correct visibilityrax=ax.inset_axes([0.0,0.0,0.12,0.2])check=CheckButtons(ax=rax,labels=lines_by_label.keys(),actives=[l.get_visible()forlinlines_by_label.values()],label_props={'color':line_colors},frame_props={'edgecolor':line_colors},check_props={'facecolor':line_colors},)defcallback(label):ln=lines_by_label[label]ln.set_visible(notln.get_visible())ln.figure.canvas.draw_idle()check.on_clicked(callback)plt.show()

References
The use of the following functions, methods, classes and modules is shownin this example: