From 266066facb4c2a74e7d11983c71eae2bcdc7b643 Mon Sep 17 00:00:00 2001 From: ianhi Date: Fri, 8 Jan 2021 23:24:02 -0500 Subject: [PATCH 1/7] Expand on slider_demo example --- examples/widgets/slider_demo.py | 72 ++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index 99db98a9185c..7b7dc29aa9e9 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -5,63 +5,70 @@ Using the slider widget to control visual properties of your plot. -In this example, a slider is used to choose the frequency of a sine -wave. You can control many continuously-varying properties of your plot in -this way. +In this example, sliders are used to control the frequency and amplitude of +a sine wave. You can control many continuously-varying properties of your plot +in this way. + +For a more detailed example of value snapping see +:doc:`/gallery/widgets/slider_snap_demo`. + +For an example of using a `matplotlib.widgets.RangeSlider` to define a range +of values see :doc:`/gallery/widgets/range_slider`. """ import numpy as np import matplotlib.pyplot as plt -from matplotlib.widgets import Slider, Button, RadioButtons +from matplotlib.widgets import Slider, Button + + +def fxn(t, amp, freq): + return amp * np.sin(2 * np.pi * freq * t) -fig, ax = plt.subplots() -plt.subplots_adjust(left=0.25, bottom=0.25) t = np.arange(0.0, 1.0, 0.001) + +# Define initial parameters a0 = 5 f0 = 3 -delta_f = 5.0 -s = a0 * np.sin(2 * np.pi * f0 * t) -l, = plt.plot(t, s, lw=2) -ax.margins(x=0) + +# Create the figure and the `~.Line2D` that we will manipulate +fig, ax = plt.subplots() +line, = plt.plot(t, fxn(t, a0, f0), lw=2) axcolor = 'lightgoldenrodyellow' +ax.margins(x=0) + +# adjust the main plot to make room for the sliders +plt.subplots_adjust(left=0.25, bottom=0.25) + +# Make a horizontal slider to control the frequency. +# This slider will snap to discrete values as defind by ``valstep``. axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) -axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor) +freq_slider = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=5.0) -sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f) -samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0) +# Make a vertically oriented slider to control the amplitude +axamp = plt.axes([0.1, 0.15, 0.03, 0.65], facecolor=axcolor) +amp_slider = Slider( + axamp, "Amp", 0.1, 10.0, valinit=a0, orientation="vertical" +) def update(val): - amp = samp.val - freq = sfreq.val - l.set_ydata(amp*np.sin(2*np.pi*freq*t)) + line.set_ydata(fxn(t, amp_slider.val, freq_slider.val)) fig.canvas.draw_idle() -sfreq.on_changed(update) -samp.on_changed(update) +freq_slider.on_changed(update) +amp_slider.on_changed(update) +# Create a `matplotlib.widgets.Button` to reset the sliders to initial values. resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975') def reset(event): - sfreq.reset() - samp.reset() + freq_slider.reset() + amp_slider.reset() button.on_clicked(reset) -rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor) -radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) - - -def colorfunc(label): - l.set_color(label) - fig.canvas.draw_idle() -radio.on_clicked(colorfunc) - -# Initialize plot with correct initial active value -colorfunc(radio.value_selected) - plt.show() ############################################################################# @@ -76,5 +83,4 @@ def colorfunc(label): import matplotlib matplotlib.widgets.Button -matplotlib.widgets.RadioButtons matplotlib.widgets.Slider From bf0530aa7040da24067f1922ed2c390c4b0b6a47 Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Sun, 10 Jan 2021 00:34:13 -0500 Subject: [PATCH 2/7] More explicit variable names Co-Authored-By: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/widgets/slider_demo.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index 7b7dc29aa9e9..ec9d75407462 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -20,18 +20,19 @@ from matplotlib.widgets import Slider, Button -def fxn(t, amp, freq): - return amp * np.sin(2 * np.pi * freq * t) +# The parametrized function to be plotted +def f(t, amplitude, frequency): + return amplitude * np.sin(2 * np.pi * frequency * t) t = np.arange(0.0, 1.0, 0.001) # Define initial parameters -a0 = 5 -f0 = 3 +init_amplitude = 5 +init_frequency = 3 # Create the figure and the `~.Line2D` that we will manipulate fig, ax = plt.subplots() -line, = plt.plot(t, fxn(t, a0, f0), lw=2) +line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2) axcolor = 'lightgoldenrodyellow' ax.margins(x=0) @@ -42,17 +43,29 @@ def fxn(t, amp, freq): # Make a horizontal slider to control the frequency. # This slider will snap to discrete values as defind by ``valstep``. axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) -freq_slider = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=5.0) +freq_slider = Slider( + ax=axfreq, + label='Frequency', + valmin=0.1, + valmax=30.0, + valinit=init_amplitude, + valstep=5.0 +) # Make a vertically oriented slider to control the amplitude axamp = plt.axes([0.1, 0.15, 0.03, 0.65], facecolor=axcolor) amp_slider = Slider( - axamp, "Amp", 0.1, 10.0, valinit=a0, orientation="vertical" + ax=axamp, + label="Amplitude", + valmin=0.1, + valmax=10.0, + valinit=init_amplitude, + orientation="vertical" ) def update(val): - line.set_ydata(fxn(t, amp_slider.val, freq_slider.val)) + line.set_ydata(f(t, amp_slider.val, freq_slider.val)) fig.canvas.draw_idle() From 786e41b2a128050db77ef7b2ff5e3acfb037cdcc Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Sun, 10 Jan 2021 00:35:17 -0500 Subject: [PATCH 3/7] Make vertical slider more nicely shaped Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/widgets/slider_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index ec9d75407462..3d85f5b1cad4 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -53,7 +53,7 @@ def f(t, amplitude, frequency): ) # Make a vertically oriented slider to control the amplitude -axamp = plt.axes([0.1, 0.15, 0.03, 0.65], facecolor=axcolor) +axamp = plt.axes([0.1, 0.25, 0.0225, 0.63], facecolor=axcolor) amp_slider = Slider( ax=axamp, label="Amplitude", From 411bdde424c85f9b77390ef27ea312bbe45a7a09 Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Sun, 10 Jan 2021 10:28:47 -0500 Subject: [PATCH 4/7] Simplify explanation and remove valstep from example --- examples/widgets/slider_demo.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index 3d85f5b1cad4..7460286175cb 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -3,13 +3,10 @@ Slider ====== -Using the slider widget to control visual properties of your plot. - In this example, sliders are used to control the frequency and amplitude of -a sine wave. You can control many continuously-varying properties of your plot -in this way. +a sine wave. -For a more detailed example of value snapping see +For an example of having the slider snap to discrete values see :doc:`/gallery/widgets/slider_snap_demo`. For an example of using a `matplotlib.widgets.RangeSlider` to define a range @@ -30,7 +27,7 @@ def f(t, amplitude, frequency): init_amplitude = 5 init_frequency = 3 -# Create the figure and the `~.Line2D` that we will manipulate +# Create the figure and the line that we will manipulate fig, ax = plt.subplots() line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2) @@ -41,7 +38,6 @@ def f(t, amplitude, frequency): plt.subplots_adjust(left=0.25, bottom=0.25) # Make a horizontal slider to control the frequency. -# This slider will snap to discrete values as defind by ``valstep``. axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) freq_slider = Slider( ax=axfreq, @@ -49,7 +45,6 @@ def f(t, amplitude, frequency): valmin=0.1, valmax=30.0, valinit=init_amplitude, - valstep=5.0 ) # Make a vertically oriented slider to control the amplitude @@ -64,11 +59,13 @@ def f(t, amplitude, frequency): ) +# The function to be called anytime a slider's value changes def update(val): line.set_ydata(f(t, amp_slider.val, freq_slider.val)) fig.canvas.draw_idle() +# register the update function with each slider freq_slider.on_changed(update) amp_slider.on_changed(update) From b0730eeae24f7cd2d7436d6a6f4c3a33bc8b0641 Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Sun, 10 Jan 2021 10:44:24 -0500 Subject: [PATCH 5/7] Link between all the slider examples. --- examples/widgets/range_slider.py | 6 ++++++ examples/widgets/slider_demo.py | 8 ++++---- examples/widgets/slider_snap_demo.py | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/widgets/range_slider.py b/examples/widgets/range_slider.py index da17e4c1d314..9442cc8a6750 100644 --- a/examples/widgets/range_slider.py +++ b/examples/widgets/range_slider.py @@ -8,6 +8,12 @@ The RangeSlider widget can be used similarly to the `.widgets.Slider` widget. The major difference is that RangeSlider's ``val`` attribute is a tuple of floats ``(lower val, upper val)`` rather than a single float. + +See :doc:`/gallery/widgets/slider_demo` for an example of using +``Slider``s to control a single float. + +See :doc:`/gallery/widgets/slider_snap_demo` for an example of having +the ``Slider``s snap to discrete values. """ import numpy as np diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index 7460286175cb..8406e0d78603 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -6,11 +6,11 @@ In this example, sliders are used to control the frequency and amplitude of a sine wave. -For an example of having the slider snap to discrete values see -:doc:`/gallery/widgets/slider_snap_demo`. +See :doc:`/gallery/widgets/slider_snap_demo` for an example of having +the ``Slider``s snap to discrete values. -For an example of using a `matplotlib.widgets.RangeSlider` to define a range -of values see :doc:`/gallery/widgets/range_slider`. +See :doc:`/gallery/widgets/range_slider` for an example of using +``RangeSlider``s to define a range of values. """ import numpy as np import matplotlib.pyplot as plt diff --git a/examples/widgets/slider_snap_demo.py b/examples/widgets/slider_snap_demo.py index 3016064b8833..9fca920f4a00 100644 --- a/examples/widgets/slider_snap_demo.py +++ b/examples/widgets/slider_snap_demo.py @@ -8,6 +8,12 @@ In this example the Freq slider is constrained to be multiples of pi, and the Amp slider uses an array as the ``valstep`` argument to more densely sample the first part of its range. + +See :doc:`/gallery/widgets/slider_demo` for an example of using +``Slider``s to control a single float. + +See :doc:`/gallery/widgets/range_slider` for an example of using +``RangeSlider``s to define a range of values. """ import numpy as np import matplotlib.pyplot as plt From fc20f19510648d31335d303ebd2778ee1a4449df Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Sun, 10 Jan 2021 11:28:33 -0500 Subject: [PATCH 6/7] Try to fix doc build --- examples/widgets/range_slider.py | 4 ++-- examples/widgets/slider_demo.py | 4 ++-- examples/widgets/slider_snap_demo.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/widgets/range_slider.py b/examples/widgets/range_slider.py index 9442cc8a6750..9dc104251786 100644 --- a/examples/widgets/range_slider.py +++ b/examples/widgets/range_slider.py @@ -10,10 +10,10 @@ is a tuple of floats ``(lower val, upper val)`` rather than a single float. See :doc:`/gallery/widgets/slider_demo` for an example of using -``Slider``s to control a single float. +a ``Slider`` to control a single float. See :doc:`/gallery/widgets/slider_snap_demo` for an example of having -the ``Slider``s snap to discrete values. +the ``Slider`` snap to discrete values. """ import numpy as np diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index 8406e0d78603..bfb49f931fc3 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -7,10 +7,10 @@ a sine wave. See :doc:`/gallery/widgets/slider_snap_demo` for an example of having -the ``Slider``s snap to discrete values. +the ``Slider`` snap to discrete values. See :doc:`/gallery/widgets/range_slider` for an example of using -``RangeSlider``s to define a range of values. +a ``RangeSlider`` to define a range of values. """ import numpy as np import matplotlib.pyplot as plt diff --git a/examples/widgets/slider_snap_demo.py b/examples/widgets/slider_snap_demo.py index 9fca920f4a00..085099c552eb 100644 --- a/examples/widgets/slider_snap_demo.py +++ b/examples/widgets/slider_snap_demo.py @@ -10,10 +10,10 @@ the first part of its range. See :doc:`/gallery/widgets/slider_demo` for an example of using -``Slider``s to control a single float. +a ``Slider`` to control a single float. See :doc:`/gallery/widgets/range_slider` for an example of using -``RangeSlider``s to define a range of values. +a ``RangeSlider`` to define a range of values. """ import numpy as np import matplotlib.pyplot as plt From d07830c7261bb93c9c80b96036bcd825e1d90037 Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Mon, 11 Jan 2021 17:16:22 -0500 Subject: [PATCH 7/7] cleanup python 2isms Co-Authored-By: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- examples/widgets/slider_demo.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/widgets/slider_demo.py b/examples/widgets/slider_demo.py index bfb49f931fc3..fade13904618 100644 --- a/examples/widgets/slider_demo.py +++ b/examples/widgets/slider_demo.py @@ -21,7 +21,7 @@ def f(t, amplitude, frequency): return amplitude * np.sin(2 * np.pi * frequency * t) -t = np.arange(0.0, 1.0, 0.001) +t = np.linspace(0, 1, 1000) # Define initial parameters init_amplitude = 5 @@ -30,6 +30,7 @@ def f(t, amplitude, frequency): # Create the figure and the line that we will manipulate fig, ax = plt.subplots() line, = plt.plot(t, f(t, init_amplitude, init_frequency), lw=2) +ax.set_xlabel('Time [s]') axcolor = 'lightgoldenrodyellow' ax.margins(x=0) @@ -41,10 +42,10 @@ def f(t, amplitude, frequency): axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) freq_slider = Slider( ax=axfreq, - label='Frequency', + label='Frequency [Hz]', valmin=0.1, - valmax=30.0, - valinit=init_amplitude, + valmax=30, + valinit=init_frequency, ) # Make a vertically oriented slider to control the amplitude @@ -52,8 +53,8 @@ def f(t, amplitude, frequency): amp_slider = Slider( ax=axamp, label="Amplitude", - valmin=0.1, - valmax=10.0, + valmin=0, + valmax=10, valinit=init_amplitude, orientation="vertical" ) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy