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

Skip to content

Commit ffc20d4

Browse files
authored
Merge pull request #9137 from jcalbert/discrete_slider
Adds option for Slider to snap to discrete values
2 parents a959c37 + 07597b1 commit ffc20d4

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Slider UI widget can snap to discrete values
2+
--------------------------------------------
3+
4+
The slider UI widget can take the optional argument *valstep*. Doing so
5+
forces the slider to take on only discrete values, starting from *valmin* and
6+
counting up to *valmax* with steps of size *valstep*.
7+
8+
If *closedmax==True*, then the slider will snap to *valmax* as well.

examples/widgets/slider_demo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
t = np.arange(0.0, 1.0, 0.001)
1414
a0 = 5
1515
f0 = 3
16+
delta_f = 5.0
1617
s = a0*np.sin(2*np.pi*f0*t)
1718
l, = plt.plot(t, s, lw=2, color='red')
1819
plt.axis([0, 1, -10, 10])
@@ -21,7 +22,7 @@
2122
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
2223
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
2324

24-
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
25+
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
2526
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
2627

2728

lib/matplotlib/widgets.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class Slider(AxesWidget):
273273
"""
274274
def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
275275
closedmin=True, closedmax=True, slidermin=None,
276-
slidermax=None, dragging=True, **kwargs):
276+
slidermax=None, dragging=True, valstep=None, **kwargs):
277277
"""
278278
Parameters
279279
----------
@@ -312,6 +312,9 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
312312
dragging : bool, optional, default: True
313313
If True the slider can be dragged by the mouse.
314314
315+
valstep : float, optional, default: None
316+
If given, the slider will snap to multiples of `valstep`.
317+
315318
Notes
316319
-----
317320
Additional kwargs are passed on to ``self.poly`` which is the
@@ -334,6 +337,7 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
334337
self.drag_active = False
335338
self.valmin = valmin
336339
self.valmax = valmax
340+
self.valstep = valstep
337341
valinit = self._value_in_bounds(valinit)
338342
if valinit is None:
339343
valinit = valmin
@@ -368,6 +372,10 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
368372

369373
def _value_in_bounds(self, val):
370374
""" Makes sure self.val is with given bounds."""
375+
if self.valstep:
376+
val = np.round((val - self.valmin)/self.valstep)*self.valstep
377+
val += self.valmin
378+
371379
if val <= self.valmin:
372380
if not self.closedmin:
373381
return
@@ -410,7 +418,7 @@ def _update(self, event):
410418
event.canvas.release_mouse(self.ax)
411419
return
412420
val = self._value_in_bounds(event.xdata)
413-
if val is not None:
421+
if (val is not None) and (val != self.val):
414422
self.set_val(val)
415423

416424
def set_val(self, val):

0 commit comments

Comments
 (0)