Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 266066f

Browse files
committed
Expand on slider_demo example
1 parent 5417682 commit 266066f

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

examples/widgets/slider_demo.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,70 @@
55
66
Using the slider widget to control visual properties of your plot.
77
8-
In this example, a slider is used to choose the frequency of a sine
9-
wave. You can control many continuously-varying properties of your plot in
10-
this way.
8+
In this example, sliders are used to control the frequency and amplitude of
9+
a sine wave. You can control many continuously-varying properties of your plot
10+
in this way.
11+
12+
For a more detailed example of value snapping see
13+
:doc:`/gallery/widgets/slider_snap_demo`.
14+
15+
For an example of using a `matplotlib.widgets.RangeSlider` to define a range
16+
of values see :doc:`/gallery/widgets/range_slider`.
1117
"""
1218
import numpy as np
1319
import matplotlib.pyplot as plt
14-
from matplotlib.widgets import Slider, Button, RadioButtons
20+
from matplotlib.widgets import Slider, Button
21+
22+
23+
def fxn(t, amp, freq):
24+
return amp * np.sin(2 * np.pi * freq * t)
1525

16-
fig, ax = plt.subplots()
17-
plt.subplots_adjust(left=0.25, bottom=0.25)
1826
t = np.arange(0.0, 1.0, 0.001)
27+
28+
# Define initial parameters
1929
a0 = 5
2030
f0 = 3
21-
delta_f = 5.0
22-
s = a0 * np.sin(2 * np.pi * f0 * t)
23-
l, = plt.plot(t, s, lw=2)
24-
ax.margins(x=0)
31+
32+
# Create the figure and the `~.Line2D` that we will manipulate
33+
fig, ax = plt.subplots()
34+
line, = plt.plot(t, fxn(t, a0, f0), lw=2)
2535

2636
axcolor = 'lightgoldenrodyellow'
37+
ax.margins(x=0)
38+
39+
# adjust the main plot to make room for the sliders
40+
plt.subplots_adjust(left=0.25, bottom=0.25)
41+
42+
# Make a horizontal slider to control the frequency.
43+
# This slider will snap to discrete values as defind by ``valstep``.
2744
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
28-
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
45+
freq_slider = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=5.0)
2946

30-
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
31-
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
47+
# Make a vertically oriented slider to control the amplitude
48+
axamp = plt.axes([0.1, 0.15, 0.03, 0.65], facecolor=axcolor)
49+
amp_slider = Slider(
50+
axamp, "Amp", 0.1, 10.0, valinit=a0, orientation="vertical"
51+
)
3252

3353

3454
def update(val):
35-
amp = samp.val
36-
freq = sfreq.val
37-
l.set_ydata(amp*np.sin(2*np.pi*freq*t))
55+
line.set_ydata(fxn(t, amp_slider.val, freq_slider.val))
3856
fig.canvas.draw_idle()
3957

4058

41-
sfreq.on_changed(update)
42-
samp.on_changed(update)
59+
freq_slider.on_changed(update)
60+
amp_slider.on_changed(update)
4361

62+
# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
4463
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
4564
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
4665

4766

4867
def reset(event):
49-
sfreq.reset()
50-
samp.reset()
68+
freq_slider.reset()
69+
amp_slider.reset()
5170
button.on_clicked(reset)
5271

53-
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
54-
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
55-
56-
57-
def colorfunc(label):
58-
l.set_color(label)
59-
fig.canvas.draw_idle()
60-
radio.on_clicked(colorfunc)
61-
62-
# Initialize plot with correct initial active value
63-
colorfunc(radio.value_selected)
64-
6572
plt.show()
6673

6774
#############################################################################
@@ -76,5 +83,4 @@ def colorfunc(label):
7683

7784
import matplotlib
7885
matplotlib.widgets.Button
79-
matplotlib.widgets.RadioButtons
8086
matplotlib.widgets.Slider

0 commit comments

Comments
 (0)