Skip to content

Fix RangeSlider for same init values #22686 #22926

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 18 commits into from
May 8, 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
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,23 @@ def handle_positions(slider):
assert_allclose(handle_positions(slider), (0.1, 0.34))


@pytest.mark.parametrize("orientation", ["horizontal", "vertical"])
def test_range_slider_same_init_values(orientation):
if orientation == "vertical":
idx = [1, 0, 3, 2]
else:
idx = [0, 1, 2, 3]

fig, ax = plt.subplots()

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


def check_polygon_selector(event_sequence, expected_result, selections_count,
**kwargs):
"""
Expand Down
67 changes: 41 additions & 26 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from . import (_api, _docstring, backend_tools, cbook, colors, ticker,
transforms)
from .lines import Line2D
from .patches import Circle, Rectangle, Ellipse
from .patches import Circle, Rectangle, Ellipse, Polygon
from .transforms import TransformedPatchPath, Affine2D


Expand Down Expand Up @@ -709,7 +709,7 @@ def __init__(
facecolor=track_color
)
ax.add_patch(self.track)
self.poly = ax.axhspan(valinit[0], valinit[1], 0, 1, **kwargs)
poly_transform = self.ax.get_yaxis_transform(which="grid")
handleXY_1 = [.5, valinit[0]]
handleXY_2 = [.5, valinit[1]]
else:
Expand All @@ -719,9 +719,15 @@ def __init__(
facecolor=track_color
)
ax.add_patch(self.track)
self.poly = ax.axvspan(valinit[0], valinit[1], 0, 1, **kwargs)
poly_transform = self.ax.get_xaxis_transform(which="grid")
handleXY_1 = [valinit[0], .5]
handleXY_2 = [valinit[1], .5]
self.poly = Polygon(np.zeros([5, 2]), **kwargs)
self._update_selection_poly(*valinit)
self.poly.set_transform(poly_transform)
self.poly.get_path()._interpolation_steps = 100
self.ax.add_patch(self.poly)
self.ax._request_autoscale_view()
self._handles = [
ax.plot(
*handleXY_1,
Expand Down Expand Up @@ -777,6 +783,27 @@ def __init__(
self._active_handle = None
self.set_val(valinit)

def _update_selection_poly(self, vmin, vmax):
"""
Update the vertices of the *self.poly* slider in-place
to cover the data range *vmin*, *vmax*.
"""
# The vertices are positioned
# 1 ------ 2
# | |
# 0, 4 ---- 3
verts = self.poly.xy
if self.orientation == "vertical":
verts[0] = verts[4] = .25, vmin
verts[1] = .25, vmax
verts[2] = .75, vmax
verts[3] = .75, vmin
else:
verts[0] = verts[4] = vmin, .25
verts[1] = vmin, .75
verts[2] = vmax, .75
verts[3] = vmax, .25

def _min_in_bounds(self, min):
"""Ensure the new min value is between valmin and self.val[1]."""
if min <= self.valmin:
Expand Down Expand Up @@ -903,36 +930,24 @@ def set_val(self, val):
"""
val = np.sort(val)
_api.check_shape((2,), val=val)
val[0] = self._min_in_bounds(val[0])
val[1] = self._max_in_bounds(val[1])
xy = self.poly.xy
vmin, vmax = val
vmin = self._min_in_bounds(vmin)
vmax = self._max_in_bounds(vmax)
self._update_selection_poly(vmin, vmax)
if self.orientation == "vertical":
xy[0] = .25, val[0]
xy[1] = .25, val[1]
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]])
self._handles[0].set_ydata([vmin])
self._handles[1].set_ydata([vmax])
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([vmin])
self._handles[1].set_xdata([vmax])

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))
self.valtext.set_text(self._format((vmin, vmax)))

if self.drawon:
self.ax.figure.canvas.draw_idle()
self.val = val
self.val = (vmin, vmax)
if self.eventson:
self._observers.process("changed", val)
self._observers.process("changed", (vmin, vmax))

def on_changed(self, func):
"""
Expand Down