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

Skip to content

Commit 0155ce4

Browse files
authored
Merge pull request #20002 from meeseeksmachine/auto-backport-of-pr-19995-on-v3.4.x
Backport PR #19995 on branch v3.4.x (Fix valinit argument to RangeSlider)
2 parents 08d9dc9 + c5ec508 commit 0155ce4

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
@@ -308,10 +308,14 @@ def test_range_slider(orientation):
308308
fig, ax = plt.subplots()
309309

310310
slider = widgets.RangeSlider(
311-
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation
311+
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation,
312+
valinit=[0.1, 0.34]
312313
)
313314
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
314-
assert_allclose(box.get_points().flatten()[idx], [0.25, 0, 0.75, 1])
315+
assert_allclose(box.get_points().flatten()[idx], [0.1, 0, 0.34, 1])
316+
317+
# Check initial value is set correctly
318+
assert_allclose(slider.val, (0.1, 0.34))
315319

316320
slider.set_val((0.2, 0.6))
317321
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)