5
5
6
6
Using the slider widget to control visual properties of your plot.
7
7
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`.
11
17
"""
12
18
import numpy as np
13
19
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 )
15
25
16
- fig , ax = plt .subplots ()
17
- plt .subplots_adjust (left = 0.25 , bottom = 0.25 )
18
26
t = np .arange (0.0 , 1.0 , 0.001 )
27
+
28
+ # Define initial parameters
19
29
a0 = 5
20
30
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 )
25
35
26
36
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``.
27
44
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 )
29
46
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
+ )
32
52
33
53
34
54
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 ))
38
56
fig .canvas .draw_idle ()
39
57
40
58
41
- sfreq .on_changed (update )
42
- samp .on_changed (update )
59
+ freq_slider .on_changed (update )
60
+ amp_slider .on_changed (update )
43
61
62
+ # Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
44
63
resetax = plt .axes ([0.8 , 0.025 , 0.1 , 0.04 ])
45
64
button = Button (resetax , 'Reset' , color = axcolor , hovercolor = '0.975' )
46
65
47
66
48
67
def reset (event ):
49
- sfreq .reset ()
50
- samp .reset ()
68
+ freq_slider .reset ()
69
+ amp_slider .reset ()
51
70
button .on_clicked (reset )
52
71
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
72
plt .show ()
66
73
67
74
#############################################################################
@@ -76,5 +83,4 @@ def colorfunc(label):
76
83
77
84
import matplotlib
78
85
matplotlib .widgets .Button
79
- matplotlib .widgets .RadioButtons
80
86
matplotlib .widgets .Slider
0 commit comments