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

Skip to content

Commit a0d25af

Browse files
committed
Fix RangeSlider.set_val when outside existing value
The clipping in the setter ensures that min <= max, but if you are setting both values outside the existing range, this will incorrectly set new minimum to old maximum and vice versa. The tests also passed accidentally because all the new ranges overlapped with the old, and did not trigger this issue. Fixes #25338
1 parent 5e8c140 commit a0d25af

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,12 +1297,12 @@ def handle_positions(slider):
12971297
else:
12981298
return [h.get_xdata()[0] for h in slider._handles]
12991299

1300-
slider.set_val((0.2, 0.6))
1301-
assert_allclose(slider.val, (0.2, 0.6))
1302-
assert_allclose(handle_positions(slider), (0.2, 0.6))
1300+
slider.set_val((0.4, 0.6))
1301+
assert_allclose(slider.val, (0.4, 0.6))
1302+
assert_allclose(handle_positions(slider), (0.4, 0.6))
13031303

13041304
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
1305-
assert_allclose(box.get_points().flatten()[idx], [0.2, .25, 0.6, .75])
1305+
assert_allclose(box.get_points().flatten()[idx], [0.4, .25, 0.6, .75])
13061306

13071307
slider.set_val((0.2, 0.1))
13081308
assert_allclose(slider.val, (0.1, 0.2))

lib/matplotlib/widgets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def __init__(
702702
valmin, valmax, valfmt, dragging, valstep)
703703

704704
# Set a value to allow _value_in_bounds() to work.
705-
self.val = [valmin, valmax]
705+
self.val = (valmin, valmax)
706706
if valinit is None:
707707
# Place at the 25th and 75th percentiles
708708
extent = valmax - valmin
@@ -947,9 +947,9 @@ def set_val(self, val):
947947
"""
948948
val = np.sort(val)
949949
_api.check_shape((2,), val=val)
950-
vmin, vmax = val
951-
vmin = self._min_in_bounds(vmin)
952-
vmax = self._max_in_bounds(vmax)
950+
# Reset value to allow _value_in_bounds() to work.
951+
self.val = (self.valmin, self.valmax)
952+
vmin, vmax = self._value_in_bounds(val)
953953
self._update_selection_poly(vmin, vmax)
954954
if self.orientation == "vertical":
955955
self._handles[0].set_ydata([vmin])

0 commit comments

Comments
 (0)