Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adds option for Slider to snap to discrete values #9137

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 12 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/2017-08-31_discrete-sliders.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Slider UI widget can snap to discrete values
--------------------------------------------

The slider UI widget can take the optional argument *valstep*. Doing so
forces the slider to take on only discrete values, starting from *valmin* and
counting up to *valmax* with steps of size *valstep*.

If *closedmax==True*, then the slider will snap to *valmax* as well.
3 changes: 2 additions & 1 deletion examples/widgets/slider_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
delta_f = 5.0
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -10, 10])
Expand All @@ -21,7 +22,7 @@
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)

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)


Expand Down
12 changes: 10 additions & 2 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Slider(AxesWidget):
"""
def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
closedmin=True, closedmax=True, slidermin=None,
slidermax=None, dragging=True, **kwargs):
slidermax=None, dragging=True, valstep=None, **kwargs):
"""
Parameters
----------
Expand Down Expand Up @@ -312,6 +312,9 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
dragging : bool, optional, default: True
If True the slider can be dragged by the mouse.

valstep : float, optional, default: None
If given, the slider will snap to multiples of `valstep`.

Notes
-----
Additional kwargs are passed on to ``self.poly`` which is the
Expand All @@ -334,6 +337,7 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
self.drag_active = False
self.valmin = valmin
self.valmax = valmax
self.valstep = valstep
valinit = self._value_in_bounds(valinit)
if valinit is None:
valinit = valmin
Expand Down Expand Up @@ -368,6 +372,10 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',

def _value_in_bounds(self, val):
""" Makes sure self.val is with given bounds."""
if self.valstep:
val = np.round((val - self.valmin)/self.valstep)*self.valstep
val += self.valmin

if val <= self.valmin:
if not self.closedmin:
return
Expand Down Expand Up @@ -410,7 +418,7 @@ def _update(self, event):
event.canvas.release_mouse(self.ax)
return
val = self._value_in_bounds(event.xdata)
if val is not None:
if (val is not None) and (val != self.val):
self.set_val(val)

def set_val(self, val):
Expand Down