Skip to content

Commit 90f98c7

Browse files
authored
Merge pull request #31476 from timhoffm/doc-improve-radio-example
DOC: Improve Radio Buttons example
2 parents 5b7f8e7 + 3af89be commit 90f98c7

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

galleries/examples/widgets/radio_buttons.py

Lines changed: 32 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,51 @@
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')
40+
axd['main'].set(xlabel="Time (s)", ylabel="Amplitude", title="Sine Wave")
3741

38-
radio_background = 'lightgoldenrodyellow'
42+
background_color = '0.95'
3943

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]},
44+
axd['freq'].set_facecolor(background_color)
45+
axd['freq'].set_title('Frequency')
46+
radio = RadioButtons(axd['freq'], labels=list(FREQUENCIES.keys()),
47+
label_props={'fontsize': [12, 14, 16]},
4348
radio_props={'s': [16, 32, 64]})
4449

4550

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

53-
ax['color'].set_facecolor(radio_background)
57+
58+
axd['color'].set_facecolor(background_color)
59+
axd['color'].set_title('Color')
5460
radio2 = RadioButtons(
55-
ax['color'], ('red', 'blue', 'green'),
61+
axd['color'], ('red', 'blue', 'green'),
5662
label_props={'color': ['red', 'blue', 'green']},
5763
radio_props={
5864
'facecolor': ['red', 'blue', 'green'],
5965
'edgecolor': ['darkred', 'darkblue', 'darkgreen'],
6066
})
6167

6268

63-
def colorfunc(label):
64-
l.set_color(label)
69+
def update_color(label):
70+
line.set_color(label)
6571
fig.canvas.draw()
66-
radio2.on_clicked(colorfunc)
72+
radio2.on_clicked(update_color)
73+
6774

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

7179

72-
def stylefunc(label):
73-
l.set_linestyle(label)
80+
def update_linestyle(label):
81+
line.set_linestyle(label)
7482
fig.canvas.draw()
75-
radio3.on_clicked(stylefunc)
83+
radio3.on_clicked(update_linestyle)
7684

7785
plt.show()
7886

0 commit comments

Comments
 (0)