3
3
Slider
4
4
======
5
5
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.
7
8
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.
11
14
"""
12
15
import numpy as np
13
16
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
15
29
30
+ # Create the figure and the line that we will manipulate
16
31
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]' )
25
34
26
35
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 )
32
37
38
+ # adjust the main plot to make room for the sliders
39
+ plt .subplots_adjust (left = 0.25 , bottom = 0.25 )
33
40
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
34
64
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 ))
38
66
fig .canvas .draw_idle ()
39
67
40
68
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 )
43
72
73
+ # Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
44
74
resetax = plt .axes ([0.8 , 0.025 , 0.1 , 0.04 ])
45
75
button = Button (resetax , 'Reset' , color = axcolor , hovercolor = '0.975' )
46
76
47
77
48
78
def reset (event ):
49
- sfreq .reset ()
50
- samp .reset ()
79
+ freq_slider .reset ()
80
+ amp_slider .reset ()
51
81
button .on_clicked (reset )
52
82
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
-
65
83
plt .show ()
66
84
67
85
#############################################################################
@@ -76,5 +94,4 @@ def colorfunc(label):
76
94
77
95
import matplotlib
78
96
matplotlib .widgets .Button
79
- matplotlib .widgets .RadioButtons
80
97
matplotlib .widgets .Slider
0 commit comments