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

Skip to content

Commit 1ba3ff1

Browse files
authored
Merge pull request #19995 from dstansby/valinit-fix
Fix valinit argument to RangeSlider
2 parents 11739ad + e32d241 commit 1ba3ff1

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,14 @@ def test_range_slider(orientation):
354354
fig, ax = plt.subplots()
355355

356356
slider = widgets.RangeSlider(
357-
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation
357+
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation,
358+
valinit=[0.1, 0.34]
358359
)
359360
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
360-
assert_allclose(box.get_points().flatten()[idx], [0.25, 0, 0.75, 1])
361+
assert_allclose(box.get_points().flatten()[idx], [0.1, 0, 0.34, 1])
362+
363+
# Check initial value is set correctly
364+
assert_allclose(slider.val, (0.1, 0.34))
361365

362366
slider.set_val((0.2, 0.6))
363367
assert_allclose(slider.val, (0.2, 0.6))

lib/matplotlib/widgets.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,13 @@ def __init__(
608608
super().__init__(ax, orientation, closedmin, closedmax,
609609
valmin, valmax, valfmt, dragging, valstep)
610610

611+
# Set a value to allow _value_in_bounds() to work.
612+
self.val = [valmin, valmax]
611613
if valinit is None:
612614
# Place at the 25th and 75th percentiles
613615
extent = valmax - valmin
614-
valinit = np.array(
615-
[valmin + extent * 0.25, valmin + extent * 0.75]
616-
)
616+
valinit = np.array([valmin + extent * 0.25,
617+
valmin + extent * 0.75])
617618
else:
618619
valinit = self._value_in_bounds(valinit)
619620
self.val = valinit
@@ -684,8 +685,9 @@ def _max_in_bounds(self, max):
684685
max = self.val[0]
685686
return self._stepped_value(max)
686687

687-
def _value_in_bounds(self, val):
688-
return (self._min_in_bounds(val[0]), self._max_in_bounds(val[1]))
688+
def _value_in_bounds(self, vals):
689+
"""Clip min, max values to the bounds."""
690+
return (self._min_in_bounds(vals[0]), self._max_in_bounds(vals[1]))
689691

690692
def _update_val_from_pos(self, pos):
691693
"""Update the slider value based on a given position."""

0 commit comments

Comments
 (0)