Note

Go to the endto download the full example code.

Radio Buttons Grid#

Using radio buttons in a 2D grid layout.

Radio buttons can be arranged in a 2D grid by passing a(rows,cols)tuple to thelayout parameter. This is useful when you have multiplerelated options that are best displayed in a grid format rather than avertical list.

In this example, we create a color picker using a 2D grid of radio buttonsto select the line color of a plot.

importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.widgetsimportRadioButtons# Generate sample datat=np.arange(0.0,2.0,0.01)s=np.sin(2*np.pi*t)fig,(ax_plot,ax_buttons)=plt.subplots(1,2,figsize=(8,4),width_ratios=[4,1.4],)# Create initial plot(line,)=ax_plot.plot(t,s,lw=2,color="red")ax_plot.set_xlabel("Time (s)")ax_plot.set_ylabel("Amplitude")ax_plot.set_title("Sine Wave - Click a color!")ax_plot.grid(True,alpha=0.3)# Configure the radio buttons axesax_buttons.set_facecolor("0.9")ax_buttons.set_title("Line Color",fontsize=12,pad=10)# Create a 2D grid of color options (3 rows x 2 columns)colors=["red","yellow","green","purple","brown","gray"]radio=RadioButtons(ax_buttons,colors,layout=(3,2))defcolor_func(label):"""Update the line color based on selected button."""line.set_color(label)fig.canvas.draw()radio.on_clicked(color_func)plt.show()
Sine Wave - Click a color!, Line Color

References

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

Tags:styling: colorstyling: conditionalplot-type: linelevel: intermediatepurpose: showcase

Gallery generated by Sphinx-Gallery