3
3
Slider
4
4
======
5
5
6
- Using the slider widget to control visual properties of your plot.
7
-
8
6
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.
7
+ a sine wave.
11
8
12
- For a more detailed example of value snapping see
9
+ For an example of having the slider snap to discrete values see
13
10
:doc:`/gallery/widgets/slider_snap_demo`.
14
11
15
12
For an example of using a `matplotlib.widgets.RangeSlider` to define a range
@@ -30,7 +27,7 @@ def f(t, amplitude, frequency):
30
27
init_amplitude = 5
31
28
init_frequency = 3
32
29
33
- # Create the figure and the `~.Line2D` that we will manipulate
30
+ # Create the figure and the line that we will manipulate
34
31
fig , ax = plt .subplots ()
35
32
line , = plt .plot (t , f (t , init_amplitude , init_frequency ), lw = 2 )
36
33
@@ -41,15 +38,13 @@ def f(t, amplitude, frequency):
41
38
plt .subplots_adjust (left = 0.25 , bottom = 0.25 )
42
39
43
40
# Make a horizontal slider to control the frequency.
44
- # This slider will snap to discrete values as defind by ``valstep``.
45
41
axfreq = plt .axes ([0.25 , 0.1 , 0.65 , 0.03 ], facecolor = axcolor )
46
42
freq_slider = Slider (
47
43
ax = axfreq ,
48
44
label = 'Frequency' ,
49
45
valmin = 0.1 ,
50
46
valmax = 30.0 ,
51
47
valinit = init_amplitude ,
52
- valstep = 5.0
53
48
)
54
49
55
50
# Make a vertically oriented slider to control the amplitude
@@ -64,11 +59,13 @@ def f(t, amplitude, frequency):
64
59
)
65
60
66
61
62
+ # The function to be called anytime a slider's value changes
67
63
def update (val ):
68
64
line .set_ydata (f (t , amp_slider .val , freq_slider .val ))
69
65
fig .canvas .draw_idle ()
70
66
71
67
68
+ # register the update function with each slider
72
69
freq_slider .on_changed (update )
73
70
amp_slider .on_changed (update )
74
71
0 commit comments