20
20
from matplotlib .widgets import Slider , Button
21
21
22
22
23
- def fxn (t , amp , freq ):
24
- return amp * np .sin (2 * np .pi * freq * t )
23
+ # The parametrized function to be plotted
24
+ def f (t , amplitude , frequency ):
25
+ return amplitude * np .sin (2 * np .pi * frequency * t )
25
26
26
27
t = np .arange (0.0 , 1.0 , 0.001 )
27
28
28
29
# Define initial parameters
29
- a0 = 5
30
- f0 = 3
30
+ init_amplitude = 5
31
+ init_frequency = 3
31
32
32
33
# Create the figure and the `~.Line2D` that we will manipulate
33
34
fig , ax = plt .subplots ()
34
- line , = plt .plot (t , fxn (t , a0 , f0 ), lw = 2 )
35
+ line , = plt .plot (t , f (t , init_amplitude , init_frequency ), lw = 2 )
35
36
36
37
axcolor = 'lightgoldenrodyellow'
37
38
ax .margins (x = 0 )
@@ -42,17 +43,29 @@ def fxn(t, amp, freq):
42
43
# Make a horizontal slider to control the frequency.
43
44
# This slider will snap to discrete values as defind by ``valstep``.
44
45
axfreq = plt .axes ([0.25 , 0.1 , 0.65 , 0.03 ], facecolor = axcolor )
45
- freq_slider = Slider (axfreq , 'Freq' , 0.1 , 30.0 , valinit = f0 , valstep = 5.0 )
46
+ freq_slider = Slider (
47
+ ax = axfreq ,
48
+ label = 'Frequency' ,
49
+ valmin = 0.1 ,
50
+ valmax = 30.0 ,
51
+ valinit = init_amplitude ,
52
+ valstep = 5.0
53
+ )
46
54
47
55
# Make a vertically oriented slider to control the amplitude
48
56
axamp = plt .axes ([0.1 , 0.15 , 0.03 , 0.65 ], facecolor = axcolor )
49
57
amp_slider = Slider (
50
- axamp , "Amp" , 0.1 , 10.0 , valinit = a0 , orientation = "vertical"
58
+ ax = axamp ,
59
+ label = "Amplitude" ,
60
+ valmin = 0.1 ,
61
+ valmax = 10.0 ,
62
+ valinit = init_amplitude ,
63
+ orientation = "vertical"
51
64
)
52
65
53
66
54
67
def update (val ):
55
- line .set_ydata (fxn (t , amp_slider .val , freq_slider .val ))
68
+ line .set_ydata (f (t , amp_slider .val , freq_slider .val ))
56
69
fig .canvas .draw_idle ()
57
70
58
71
0 commit comments