Skip to content

RangeSlider handle set_val bugfix #22711

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 2 commits into from
Mar 30, 2022
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
13 changes: 12 additions & 1 deletion lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,19 +1105,30 @@ def test_range_slider(orientation):
# Check initial value is set correctly
assert_allclose(slider.val, (0.1, 0.34))

def handle_positions(slider):
if orientation == "vertical":
return [h.get_ydata()[0] for h in slider._handles]
else:
return [h.get_xdata()[0] for h in slider._handles]

slider.set_val((0.2, 0.6))
assert_allclose(slider.val, (0.2, 0.6))
assert_allclose(handle_positions(slider), (0.2, 0.6))

box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
assert_allclose(box.get_points().flatten()[idx], [0.2, .25, 0.6, .75])

slider.set_val((0.2, 0.1))
assert_allclose(slider.val, (0.1, 0.2))
assert_allclose(handle_positions(slider), (0.1, 0.2))

slider.set_val((-1, 10))
assert_allclose(slider.val, (0, 1))
assert_allclose(handle_positions(slider), (0, 1))

slider.reset()
assert_allclose(slider.val, [0.1, 0.34])
assert_allclose(slider.val, (0.1, 0.34))
assert_allclose(handle_positions(slider), (0.1, 0.34))


def check_polygon_selector(event_sequence, expected_result, selections_count,
Expand Down
24 changes: 20 additions & 4 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,10 @@ def _update_val_from_pos(self, pos):
val = self._max_in_bounds(pos)
self.set_max(val)
if self._active_handle:
self._active_handle.set_xdata([val])
if self.orientation == "vertical":
self._active_handle.set_ydata([val])
else:
self._active_handle.set_xdata([val])

def _update(self, event):
"""Update the slider position."""
Expand All @@ -836,11 +839,16 @@ def _update(self, event):
return

# determine which handle was grabbed
handle = self._handles[
np.argmin(
if self.orientation == "vertical":
handle_index = np.argmin(
np.abs([h.get_ydata()[0] - event.ydata for h in self._handles])
)
else:
handle_index = np.argmin(
np.abs([h.get_xdata()[0] - event.xdata for h in self._handles])
)
]
handle = self._handles[handle_index]

# these checks ensure smooth behavior if the handles swap which one
# has a higher value. i.e. if one is dragged over and past the other.
if handle is not self._active_handle:
Expand Down Expand Up @@ -904,14 +912,22 @@ def set_val(self, val):
xy[2] = .75, val[1]
xy[3] = .75, val[0]
xy[4] = .25, val[0]

self._handles[0].set_ydata([val[0]])
self._handles[1].set_ydata([val[1]])
else:
xy[0] = val[0], .25
xy[1] = val[0], .75
xy[2] = val[1], .75
xy[3] = val[1], .25
xy[4] = val[0], .25

self._handles[0].set_xdata([val[0]])
self._handles[1].set_xdata([val[1]])

self.poly.xy = xy
self.valtext.set_text(self._format(val))

if self.drawon:
self.ax.figure.canvas.draw_idle()
self.val = val
Expand Down