8000 Expand on slider_demo example by ianhi · Pull Request #19264 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Expand on slider_demo example #19264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 11, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions examples/widgets/slider_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

#############################################################################
Expand All @@ -76,5 +83,4 @@ def colorfunc(label):

import matplotlib
matplotlib.widgets.Button
matplotlib.widgets.RadioButtons
matplotlib.widgets.Slider
0