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

Skip to content

Commit 47dcf54

Browse files
ianhitimhoffm
andauthored
Expand on slider_demo example (#19264)
* Expand on slider_demo example * More explicit variable names Co-Authored-By: Tim Hoffmann <[email protected]> * Make vertical slider more nicely shaped Co-authored-by: Tim Hoffmann <[email protected]> * Simplify explanation and remove valstep from example * Link between all the slider examples. * Try to fix doc build * cleanup python 2isms Co-Authored-By: Tim Hoffmann <[email protected]> Co-authored-by: Tim Hoffmann <[email protected]>
1 parent 663358e commit 47dcf54

File tree

3 files changed

+67
-38
lines changed

3 files changed

+67
-38
lines changed

examples/widgets/range_slider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
The RangeSlider widget can be used similarly to the `.widgets.Slider`
99
widget. The major difference is that RangeSlider's ``val`` attribute
1010
is a tuple of floats ``(lower val, upper val)`` rather than a single float.
11+
12+
See :doc:`/gallery/widgets/slider_demo` for an example of using
13+
a ``Slider`` to control a single float.
14+
15+
See :doc:`/gallery/widgets/slider_snap_demo` for an example of having
16+
the ``Slider`` snap to discrete values.
1117
"""
1218

1319
import numpy as np

examples/widgets/slider_demo.py

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,83 @@
33
Slider
44
======
55
6-
Using the slider widget to control visual properties of your plot.
6+
In this example, sliders are used to control the frequency and amplitude of
7+
a sine wave.
78
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.
9+
See :doc:`/gallery/widgets/slider_snap_demo` for an example of having
10+
the ``Slider`` snap to discrete values.
11+
12+
See :doc:`/gallery/widgets/range_slider` for an example of using
13+
a ``RangeSlider`` to define a range of values.
1114
"""
1215
import numpy as np
1316
import matplotlib.pyplot as plt
14-
from matplotlib.widgets import Slider, Button, RadioButtons
17+
from matplotlib.widgets import Slider, Button
18+
19+
20+
# The parametrized function to be plotted
21+
def f(t, amplitude, frequency):
22+
return amplitude * np.sin(2 * np.pi * frequency * t)
23+
24+
t = np.linspace(0, 1, 1000)
25+
26+
# Define initial parameters
27+
init_amplitude = 5
28+
init_frequency = 3
1529

30+
# Create the figure and the line that we will manipulate
1631
fig, ax = plt.subplots()
17-
plt.subplots_adjust(left=0.25, bottom=0.25)
18-
t = np.arange(0.0, 1.0, 0.001)
19-
a0 = 5
20-
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)
32+
line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2)
33+
ax.set_xlabel('Time [s]')
2534

2635
axcolor = 'lightgoldenrodyellow'
27-
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)
29-
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)
36+
ax.margins(x=0)
3237

38+
# adjust the main plot to make room for the sliders
39+
plt.subplots_adjust(left=0.25, bottom=0.25)
3340

41+
# Make a horizontal slider to control the frequency.
42+
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
43+
freq_slider = Slider(
44+
ax=axfreq,
45+
label='Frequency [Hz]',
46+
valmin=0.1,
47+
valmax=30,
48+
valinit=init_frequency,
49+
)
50+
51+
# Make a vertically oriented slider to control the amplitude
52+
axamp = plt.axes([0.1, 0.25, 0.0225, 0.63], facecolor=axcolor)
53+
amp_slider = Slider(
54+
ax=axamp,
55+
label="Amplitude",
56+
valmin=0,
57+
valmax=10,
58+
valinit=init_amplitude,
59+
orientation="vertical"
60+
)
61+
62+
63+
# The function to be called anytime a slider's value changes
3464
def update(val):
35-
amp = samp.val
36-
freq = sfreq.val
37-
l.set_ydata(amp*np.sin(2*np.pi*freq*t))
65+
line.set_ydata(f(t, amp_slider.val, freq_slider.val))
3866
fig.canvas.draw_idle()
3967

4068

41-
sfreq.on_changed(update)
42-
samp.on_changed(update)
69+
# register the update function with each slider
70+
freq_slider.on_changed(update)
71+
amp_slider.on_changed(update)
4372

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

4777

4878
def reset(event):
49-
sfreq.reset()
50-
samp.reset()
79+
freq_slider.reset()
80+
amp_slider.reset()
5181
button.on_clicked(reset)
5282

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-
6583
plt.show()
6684

6785
#############################################################################
@@ -76,5 +94,4 @@ def colorfunc(label):
7694

7795
import matplotlib
7896
matplotlib.widgets.Button
79-
matplotlib.widgets.RadioButtons
8097
matplotlib.widgets.Slider

examples/widgets/slider_snap_demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
In this example the Freq slider is constrained to be multiples of pi, and the
99
Amp slider uses an array as the ``valstep`` argument to more densely sample
1010
the first part of its range.
11+
12+
See :doc:`/gallery/widgets/slider_demo` for an example of using
13+
a ``Slider`` to control a single float.
14+
15+
See :doc:`/gallery/widgets/range_slider` for an example of using
16+
a ``RangeSlider`` to define a range of values.
1117
"""
1218
import numpy as np
1319
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)