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

Skip to content

Backport PR #19995 on branch v3.4.x (Fix valinit argument to RangeSlider) #20002

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
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
8 changes: 6 additions & 2 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,14 @@ def test_range_slider(orientation):
fig, ax = plt.subplots()

slider = widgets.RangeSlider(
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation,
valinit=[0.1, 0.34]
)
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
assert_allclose(box.get_points().flatten()[idx], [0.25, 0, 0.75, 1])
assert_allclose(box.get_points().flatten()[idx], [0.1, 0, 0.34, 1])

# Check initial value is set correctly
assert_allclose(slider.val, (0.1, 0.34))

slider.set_val((0.2, 0.6))
assert_allclose(slider.val, (0.2, 0.6))
Expand Down
12 changes: 7 additions & 5 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,13 @@ def __init__(
super().__init__(ax, orientation, closedmin, closedmax,
valmin, valmax, valfmt, dragging, valstep)

# Set a value to allow _value_in_bounds() to work.
self.val = [valmin, valmax]
if valinit is None:
# Place at the 25th and 75th percentiles
extent = valmax - valmin
valinit = np.array(
[valmin + extent * 0.25, valmin + extent * 0.75]
)
valinit = np.array([valmin + extent * 0.25,
valmin + extent * 0.75])
else:
valinit = self._value_in_bounds(valinit)
self.val = valinit
Expand Down Expand Up @@ -684,8 +685,9 @@ def _max_in_bounds(self, max):
max = self.val[0]
return self._stepped_value(max)

def _value_in_bounds(self, val):
return (self._min_in_bounds(val[0]), self._max_in_bounds(val[1]))
def _value_in_bounds(self, vals):
"""Clip min, max values to the bounds."""
return (self._min_in_bounds(vals[0]), self._max_in_bounds(vals[1]))

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