Skip to content

Commit c01df7f

Browse files
committed
DOC: Improve Radio Buttons example
Several small changes.
1 parent 48a7766 commit c01df7f

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

galleries/examples/widgets/radio_buttons.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
from matplotlib.widgets import RadioButtons
2121

22+
FREQUENCIES = {'1 Hz': 1, '2 Hz': 2, '4 Hz': 4}
23+
2224
t = np.arange(0.0, 2.0, 0.01)
23-
s0 = np.sin(2*np.pi*t)
24-
s1 = np.sin(4*np.pi*t)
25-
s2 = np.sin(8*np.pi*t)
2625

27-
fig, ax = plt.subplot_mosaic(
26+
27+
def f(t, freq):
28+
return np.sin(2*np.pi*freq*t)
29+
30+
fig, axd = plt.subplot_mosaic(
2831
[
2932
['main', 'freq'],
3033
['main', 'color'],
@@ -33,46 +36,50 @@
3336
width_ratios=[5, 1],
3437
layout='constrained',
3538
)
36-
l, = ax['main'].plot(t, s0, lw=2, color='red')
39+
(line,) = axd['main'].plot(t, f(t, freq=1), lw=2, color='red')
3740

38-
radio_background = 'lightgoldenrodyellow'
41+
background_color = '0.95'
3942

40-
ax['freq'].set_facecolor(radio_background)
41-
radio = RadioButtons(ax['freq'], ('1 Hz', '2 Hz', '4 Hz'),
42-
label_props={'color': 'cmy', 'fontsize': [12, 14, 16]},
43+
axd['freq'].set_facecolor(background_color)
44+
axd['freq'].set_title('Frequency')
45+
radio = RadioButtons(axd['freq'], labels=list(FREQUENCIES.keys()),
46+
label_props={'fontsize': [12, 14, 16]},
4347
radio_props={'s': [16, 32, 64]})
4448

4549

46-
def hzfunc(label):
47-
hzdict = {'1 Hz': s0, '2 Hz': s1, '4 Hz': s2}
48-
ydata = hzdict[label]
49-
l.set_ydata(ydata)
50+
def update_frequency(label):
51+
ydata = f(t, freq=FREQUENCIES[label])
52+
line.set_ydata(ydata)
5053
fig.canvas.draw()
51-
radio.on_clicked(hzfunc)
54+
radio.on_clicked(update_frequency)
5255

53-
ax['color'].set_facecolor(radio_background)
56+
57+
axd['color'].set_facecolor(background_color)
58+
axd['color'].set_title('Color')
5459
radio2 = RadioButtons(
55-
ax['color'], ('red', 'blue', 'green'),
60+
axd['color'], ('red', 'blue', 'green'),
5661
label_props={'color': ['red', 'blue', 'green']},
5762
radio_props={
5863
'facecolor': ['red', 'blue', 'green'],
5964
'edgecolor': ['darkred', 'darkblue', 'darkgreen'],
6065
})
6166

6267

63-
def colorfunc(label):
64-
l.set_color(label)
68+
def update_color(label):
69+
line.set_color(label)
6570
fig.canvas.draw()
66-
radio2.on_clicked(colorfunc)
71+
radio2.on_clicked(update_color)
72+
6773

68-
ax['linestyle'].set_facecolor(radio_background)
69-
radio3 = RadioButtons(ax['linestyle'], ('-', '--', '-.', ':'))
74+
axd['linestyle'].set_facecolor(background_color)
75+
axd['linestyle'].set_title('Linestyle')
76+
radio3 = RadioButtons(axd['linestyle'], ('solid', 'dashed', 'dashdot', 'dotted'))
7077

7178

72-
def stylefunc(label):
73-
l.set_linestyle(label)
79+
def update_linestyle(label):
80+
line.set_linestyle(label)
7481
fig.canvas.draw()
75-
radio3.on_clicked(stylefunc)
82+
radio3.on_clicked(update_linestyle)
7683

7784
plt.show()
7885

0 commit comments

Comments
 (0)