|
23 | 23 | t = np.arange(0.0, 2.0, 0.01) |
24 | 24 | s = np.sin(2 * np.pi * t) |
25 | 25 |
|
26 | | -fig, (ax_plot, ax_buttons) = plt.subplots( |
| 26 | +fig, (ax, ax_buttons) = plt.subplots( |
27 | 27 | 1, |
28 | 28 | 2, |
29 | 29 | figsize=(8, 4), |
30 | 30 | width_ratios=[4, 1.4], |
31 | 31 | ) |
32 | 32 |
|
33 | 33 | # Create initial plot |
34 | | -(line,) = ax_plot.plot(t, s, lw=2, color="red") |
35 | | -ax_plot.set_xlabel("Time (s)") |
36 | | -ax_plot.set_ylabel("Amplitude") |
37 | | -ax_plot.set_title("Sine Wave - Click a color!") |
38 | | -ax_plot.grid(True, alpha=0.3) |
| 34 | +(line,) = ax.plot(t, s, lw=2, color="red") |
| 35 | +ax.set(xlabel="Time (s)", ylabel="Amplitude", title="Sine Wave - Click a color!") |
39 | 36 |
|
40 | 37 | # Configure the radio buttons axes |
41 | | -ax_buttons.set_facecolor("0.9") |
42 | | -ax_buttons.set_title("Line Color", fontsize=12, pad=10) |
| 38 | +ax_buttons.set_facecolor("0.95") |
| 39 | +ax_buttons.set_title("Line Color") |
43 | 40 | # Create a 2D grid of color options (3 rows x 2 columns) |
44 | 41 | colors = ["red", "yellow", "green", "purple", "brown", "gray"] |
45 | 42 | radio = RadioButtons(ax_buttons, colors, layout=(3, 2)) |
46 | 43 |
|
47 | 44 |
|
48 | | -def color_func(label): |
| 45 | +def update_color(label): |
49 | 46 | """Update the line color based on selected button.""" |
50 | 47 | line.set_color(label) |
51 | 48 | fig.canvas.draw() |
| 49 | +radio.on_clicked(update_color) |
52 | 50 |
|
53 | 51 |
|
54 | | -radio.on_clicked(color_func) |
55 | | - |
56 | 52 | plt.show() |
57 | 53 |
|
58 | 54 | # %% |
|
0 commit comments