From d950cb141f747d5e708419d989dc33b18bc07653 Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Wed, 10 Nov 2021 11:26:05 -0500 Subject: [PATCH] Fix RangeSlider.reset Previously would error as the truth value of an array is ambiguous. --- lib/matplotlib/tests/test_widgets.py | 11 +++++++++++ lib/matplotlib/widgets.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_widgets.py b/lib/matplotlib/tests/test_widgets.py index f50402a20a15..0e4a7e7daa68 100644 --- a/lib/matplotlib/tests/test_widgets.py +++ b/lib/matplotlib/tests/test_widgets.py @@ -743,6 +743,14 @@ def test_slider_horizontal_vertical(): assert_allclose(box.bounds, [.25, 0, .5, 10/24]) +def test_slider_reset(): + fig, ax = plt.subplots() + slider = widgets.Slider(ax=ax, label='', valmin=0, valmax=1, valinit=.5) + slider.set_val(0.75) + slider.reset() + assert slider.val == 0.5 + + @pytest.mark.parametrize("orientation", ["horizontal", "vertical"]) def test_range_slider(orientation): if orientation == "vertical": @@ -773,6 +781,9 @@ def test_range_slider(orientation): slider.set_val((-1, 10)) assert_allclose(slider.val, (0, 1)) + slider.reset() + assert_allclose(slider.val, [0.1, 0.34]) + def check_polygon_selector(event_sequence, expected_result, selections_count): """ diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index d9a095f1a20f..1fcc52198c99 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -305,7 +305,7 @@ def disconnect(self, cid): def reset(self): """Reset the slider to the initial value.""" - if self.val != self.valinit: + if np.any(self.val != self.valinit): self.set_val(self.valinit)