Skip to content

Added a new public API update_range to Slider widget. It helps to change the valmin and valmax of the Slider #20579

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,45 @@ def test_slider_valmin_valmax():
assert slider.val == slider.valmax


def test_slider_set_limits():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=10.0, valmax=40.0,
valinit=15.0)
slider.set_limits(vmin=10, vmax=50)
assert slider.valinit == 15
assert slider.valmax == 50
assert slider.valmin == 10

slider = widgets.Slider(ax=ax, label='', valmin=10.0, valmax=40.0,
valinit=15.0)
slider.set_limits(vmin=20, vmax=40)
assert slider.valinit == 20
assert slider.valmax == 40
assert slider.valmin == 20

slider = widgets.Slider(ax=ax, label='', valmin=10.0, valmax=40.0,
valinit=15.0)
slider.val = 20
slider.set_limits(vmin=30, vmax=50)
assert slider.val == 30
assert slider.valmax == 50
assert slider.valmin == 30

slider = widgets.Slider(ax=ax, label='', valmin=10.0, valmax=40.0,
valinit=15.0)
slider.val = 20
slider.set_limits(vmin=1, vmax=7)
assert slider.val == 7
assert slider.valmax == 7
assert slider.valmin == 1

slider = widgets.Slider(ax=ax, label='', valmin=10.0, valmax=40.0,
valinit=15.0)
slider.set_limits()
assert slider.valmax == 40
assert slider.valmin == 10


def test_slider_valstep_snapping():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
Expand Down
25 changes: 25 additions & 0 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,19 @@ def reset(self):
if self.val != self.valinit:
self.set_val(self.valinit)

def set_limits(self, vmin=None, vmax=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match __init__, these should probably be named:

Suggested change
def set_limits(self, vmin=None, vmax=None):
def set_limits(self, valmin=None, valmax=None):

"""Update the limits of the slider."""
if vmin is None and vmax is None:
return
if vmin is not None:
self.valmin = vmin
if vmax is not None:
self.valmax = vmax
if self.orientation == 'vertical':
self.ax.set_ylim((self.valmin, self.valmax))
else:
self.ax.set_xlim((self.valmin, self.valmax))


class Slider(SliderBase):
"""
Expand Down Expand Up @@ -583,6 +596,18 @@ def on_changed(self, func):
"""
return self._observers.connect('changed', lambda val: func(val))

def set_limits(self, vmin=None, vmax=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about argument names.

"""Update the limits of the slider."""
super().set_limits(vmin=vmin, vmax=vmax)
self.val = self._value_in_bounds(self.val)
# if we reset the slider after updating the limits then we should have
# the proper valinit value
self.valinit = self._value_in_bounds(self.valinit)
if self.orientation == 'vertical':
self.hline.set_ydata(self.valinit)
else:
self.vline.set_xdata(self.valinit)


class RangeSlider(SliderBase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RangeSlider also need some custom set_limits, or is the base class implementation enough?

"""
Expand Down