88Radio buttons let you choose between multiple options in a visualization.
99In this case, the buttons let the user choose one of the three different sine
1010waves to be shown in the plot.
11+
12+ Radio buttons may be styled using the *label_props* and *radio_props* parameters, which
13+ each take a dictionary with keys of artist property names and values of lists of
14+ settings with length matching the number of buttons.
1115"""
1216
1317import matplotlib .pyplot as plt
2024s1 = np .sin (4 * np .pi * t )
2125s2 = np .sin (8 * np .pi * t )
2226
23- fig , ax = plt .subplots ()
24- l , = ax .plot (t , s0 , lw = 2 , color = 'red' )
25- fig .subplots_adjust (left = 0.3 )
26-
27- axcolor = 'lightgoldenrodyellow'
28- rax = fig .add_axes ([0.05 , 0.7 , 0.15 , 0.15 ], facecolor = axcolor )
29- radio = RadioButtons (rax , ('1 Hz' , '2 Hz' , '4 Hz' ),
27+ fig , ax = plt .subplot_mosaic (
28+ [
29+ ['main' , 'freq' ],
30+ ['main' , 'color' ],
31+ ['main' , 'linestyle' ],
32+ ],
33+ width_ratios = [5 , 1 ],
34+ layout = 'constrained' ,
35+ )
36+ l , = ax ['main' ].plot (t , s0 , lw = 2 , color = 'red' )
37+
38+ radio_background = 'lightgoldenrodyellow'
39+
40+ ax ['freq' ].set_facecolor (radio_background )
41+ radio = RadioButtons (ax ['freq' ], ('1 Hz' , '2 Hz' , '4 Hz' ),
3042 label_props = {'color' : 'cmy' , 'fontsize' : [12 , 14 , 16 ]},
3143 radio_props = {'s' : [16 , 32 , 64 ]})
3244
@@ -38,9 +50,9 @@ def hzfunc(label):
3850 fig .canvas .draw ()
3951radio .on_clicked (hzfunc )
4052
41- rax = fig . add_axes ([ 0.05 , 0.4 , 0.15 , 0.15 ], facecolor = axcolor )
53+ ax [ 'color' ]. set_facecolor ( radio_background )
4254radio2 = RadioButtons (
43- rax , ('red' , 'blue' , 'green' ),
55+ ax [ 'color' ] , ('red' , 'blue' , 'green' ),
4456 label_props = {'color' : ['red' , 'blue' , 'green' ]},
4557 radio_props = {
4658 'facecolor' : ['red' , 'blue' , 'green' ],
@@ -53,8 +65,8 @@ def colorfunc(label):
5365 fig .canvas .draw ()
5466radio2 .on_clicked (colorfunc )
5567
56- rax = fig . add_axes ([ 0.05 , 0.1 , 0.15 , 0.15 ], facecolor = axcolor )
57- radio3 = RadioButtons (rax , ('-' , '--' , '-.' , ':' ))
68+ ax [ 'linestyle' ]. set_facecolor ( radio_background )
69+ radio3 = RadioButtons (ax [ 'linestyle' ] , ('-' , '--' , '-.' , ':' ))
5870
5971
6072def stylefunc (label ):
0 commit comments