8
8
Radio buttons let you choose between multiple options in a visualization.
9
9
In this case, the buttons let the user choose one of the three different sine
10
10
waves 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.
11
15
"""
12
16
13
17
import matplotlib .pyplot as plt
20
24
s1 = np .sin (4 * np .pi * t )
21
25
s2 = np .sin (8 * np .pi * t )
22
26
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' ),
30
42
label_props = {'color' : 'cmy' , 'fontsize' : [12 , 14 , 16 ]},
31
43
radio_props = {'s' : [16 , 32 , 64 ]})
32
44
@@ -38,9 +50,9 @@ def hzfunc(label):
38
50
fig .canvas .draw ()
39
51
radio .on_clicked (hzfunc )
40
52
41
- rax = fig . add_axes ([ 0.05 , 0.4 , 0.15 , 0.15 ], facecolor = axcolor )
53
+ ax [ 'color' ]. set_facecolor ( radio_background )
42
54
radio2 = RadioButtons (
43
- rax , ('red' , 'blue' , 'green' ),
55
+ ax [ 'color' ] , ('red' , 'blue' , 'green' ),
44
56
label_props = {'color' : ['red' , 'blue' , 'green' ]},
45
57
radio_props = {
46
58
'facecolor' : ['red' , 'blue' , 'green' ],
@@ -53,8 +65,8 @@ def colorfunc(label):
53
65
fig .canvas .draw ()
54
66
radio2 .on_clicked (colorfunc )
55
67
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' ] , ('-' , '--' , '-.' , ':' ))
58
70
59
71
60
72
def stylefunc (label ):
0 commit comments