-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathradio_buttons_grid.py
More file actions
70 lines (56 loc) · 1.65 KB
/
radio_buttons_grid.py
File metadata and controls
70 lines (56 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
==================
Radio Buttons Grid
==================
Using radio buttons in a 2D grid layout.
Radio buttons can be arranged in a 2D grid by passing a ``(rows, cols)``
tuple to the *layout* parameter. This is useful when you have multiple
related options that are best displayed in a grid format rather than a
vertical list.
In this example, we create a color picker using a 2D grid of radio buttons
to select the line color of a plot.
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import RadioButtons
# Generate sample data
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, (ax, ax_buttons) = plt.subplots(
1,
2,
figsize=(8, 4),
width_ratios=[4, 1.4],
)
# Create initial plot
(line,) = ax.plot(t, s, lw=2, color="red")
ax.set(xlabel="Time (s)", ylabel="Amplitude", title="Sine Wave - Click a color!")
# Configure the radio buttons axes
ax_buttons.set_facecolor("0.95")
ax_buttons.spines[:].set_color("0.8")
ax_buttons.set_title("Line Color")
# Create a 2D grid of color options (3 rows x 2 columns)
colors = ["red", "yellow", "green", "purple", "brown", "gray"]
radio = RadioButtons(ax_buttons, colors, layout=(3, 2))
def update_color(label):
"""Update the line color based on selected button."""
line.set_color(label)
fig.canvas.draw()
radio.on_clicked(update_color)
plt.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.widgets.RadioButtons`
#
# .. tags::
#
# styling: color
# styling: conditional
# plot-type: line
# level: intermediate
# purpose: showcase