Skip to content

Improve button widget examples a bit #26427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions galleries/examples/widgets/check_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
This program shows the use of `.CheckButtons` which is similar to
check boxes. There are 3 different sine waves shown, and we can choose which
waves are displayed with the check buttons.

Check buttons may be styled using the *check_props*, *frame_props*, and *label_props*
parameters. The parameters each take a dictionary with keys of artist property names and
values of lists of settings with length matching the number of buttons.
"""

import matplotlib.pyplot as plt
Expand All @@ -24,13 +28,12 @@
l0, = ax.plot(t, s0, visible=False, lw=2, color='black', label='1 Hz')
l1, = ax.plot(t, s1, lw=2, color='red', label='2 Hz')
l2, = ax.plot(t, s2, lw=2, color='green', label='3 Hz')
fig.subplots_adjust(left=0.2)

lines_by_label = {l.get_label(): l for l in [l0, l1, l2]}
line_colors = [l.get_color() for l in lines_by_label.values()]

# Make checkbuttons with all plotted lines with correct visibility
rax = fig.add_axes([0.05, 0.4, 0.1, 0.15])
rax = ax.inset_axes([0.0, 0.0, 0.12, 0.2])
check = CheckButtons(
ax=rax,
labels=lines_by_label.keys(),
Expand Down
34 changes: 23 additions & 11 deletions galleries/examples/widgets/radio_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Radio buttons let you choose between multiple options in a visualization.
In this case, the buttons let the user choose one of the three different sine
waves to be shown in the plot.

Radio buttons may be styled using the *label_props* and *radio_props* parameters, which
each take a dictionary with keys of artist property names and values of lists of
settings with length matching the number of buttons.
"""

import matplotlib.pyplot as plt
Expand All @@ -20,13 +24,21 @@
s1 = np.sin(4*np.pi*t)
s2 = np.sin(8*np.pi*t)

fig, ax = plt.subplots()
l, = ax.plot(t, s0, lw=2, color='red')
fig.subplots_adjust(left=0.3)

axcolor = 'lightgoldenrodyellow'
rax = fig.add_axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('1 Hz', '2 Hz', '4 Hz'),
fig, ax = plt.subplot_mosaic(
[
['main', 'freq'],
['main', 'color'],
['main', 'linestyle'],
],
width_ratios=[5, 1],
layout='constrained',
)
l, = ax['main'].plot(t, s0, lw=2, color='red')

radio_background = 'lightgoldenrodyellow'

ax['freq'].set_facecolor(radio_background)
radio = RadioButtons(ax['freq'], ('1 Hz', '2 Hz', '4 Hz'),
label_props={'color': 'cmy', 'fontsize': [12, 14, 16]},
radio_props={'s': [16, 32, 64]})

Expand All @@ -38,9 +50,9 @@ def hzfunc(label):
fig.canvas.draw()
radio.on_clicked(hzfunc)

rax = fig.add_axes([0.05, 0.4, 0.15, 0.15], facecolor=axcolor)
ax['color'].set_facecolor(radio_background)
radio2 = RadioButtons(
rax, ('red', 'blue', 'green'),
ax['color'], ('red', 'blue', 'green'),
label_props={'color': ['red', 'blue', 'green']},
radio_props={
'facecolor': ['red', 'blue', 'green'],
Expand All @@ -53,8 +65,8 @@ def colorfunc(label):
fig.canvas.draw()
radio2.on_clicked(colorfunc)

rax = fig.add_axes([0.05, 0.1, 0.15, 0.15], facecolor=axcolor)
radio3 = RadioButtons(rax, ('-', '--', '-.', ':'))
ax['linestyle'].set_facecolor(radio_background)
radio3 = RadioButtons(ax['linestyle'], ('-', '--', '-.', ':'))


def stylefunc(label):
Expand Down