33Slider
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"""
1215import numpy as np
1316import 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
1631fig , 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
2635axcolor = '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
3464def 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.
4474resetax = plt .axes ([0.8 , 0.025 , 0.1 , 0.04 ])
4575button = Button (resetax , 'Reset' , color = axcolor , hovercolor = '0.975' )
4676
4777
4878def reset (event ):
49- sfreq .reset ()
50- samp .reset ()
79+ freq_slider .reset ()
80+ amp_slider .reset ()
5181button .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-
6583plt .show ()
6684
6785#############################################################################
@@ -76,5 +94,4 @@ def colorfunc(label):
7694
7795import matplotlib
7896matplotlib .widgets .Button
79- matplotlib .widgets .RadioButtons
8097matplotlib .widgets .Slider
0 commit comments