8000 Added a new public API update_range to Slider widget. It helps to change the valmin and valmax of the Slider by BSNayal · Pull Request #20579 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

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 3 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
8000
Diff view
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@ def test_slider_valmin_valmax():
assert slider.val == slider.valmax


def test_slider_update_valmin_valmax():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=10.0)
slider.update_range(vmin=20, vmax=50)
assert slider.val == slider.valmin

def test_slider_valstep_snapping():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,24 @@ def on_changed(self, func):
"""
return self._observers.connect('changed', lambda val: func(val))

def update_range(self, vmin=None, vmax=None):
"""Update the range of the slider"""
if not vmin and not vmax:
raise ValueError(
(f"Argument vmin ({type(vmin)}) has no value"
f"Argument vmax ({type(vmax)}) has no value"))
if vmin:
self.valmin = vmin
if vmax:
self.valmax = vmax
self.val = self._value_in_bounds(self.valinit)
if self.orientation == 'vertical':
self.ax.set_ylim((self.valmin, self.valmax))
self.hline.set_ydata(self.val)
else:
self.ax.set_xlim((self.valmin, self.valmax))
self.vline.set_xdata(self.val)


class CheckButtons(AxesWidget):
r"""
Expand Down
0