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

Skip to content

Commit bed2362

Browse files
Fix RangeSlider for same init values
Closes #22686. Co-authored-by: Nickolaos Giannatos <[email protected]>
1 parent 65e9620 commit bed2362

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,23 @@ def handle_positions(slider):
11611161
assert_allclose(handle_positions(slider), (0.1, 0.34))
11621162

11631163

1164+
@pytest.mark.parametrize("orientation", ["horizontal", "vertical"])
1165+
def test_range_slider_same_init_values(orientation):
1166+
if orientation == "vertical":
1167+
idx = [1, 0, 3, 2]
1168+
else:
1169+
idx = [0, 1, 2, 3]
1170+
1171+
fig, ax = plt.subplots()
1172+
1173+
slider = widgets.RangeSlider(
1174+
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation,
1175+
valinit=[0, 0]
1176+
)
1177+
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
1178+
assert_allclose(box.get_points().flatten()[idx], [0, 0.25, 0, 0.75])
1179+
1180+
11641181
def check_polygon_selector(event_sequence, expected_result, selections_count,
11651182
**kwargs):
11661183
"""

lib/matplotlib/widgets.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import (_api, _docstring, backend_tools, cbook, colors, ticker,
2020
transforms)
2121
from .lines import Line2D
22-
from .patches import Circle, Rectangle, Ellipse
22+
from .patches import Circle, Rectangle, Ellipse, Polygon
2323
from .transforms import TransformedPatchPath, Affine2D
2424

2525

@@ -709,7 +709,7 @@ def __init__(
709709
facecolor=track_color
710710
)
711711
ax.add_patch(self.track)
712-
self.poly = ax.axhspan(valinit[0], valinit[1], 0, 1, **kwargs)
712+
poly_transform = self.ax.get_yaxis_transform(which="grid")
713713
handleXY_1 = [.5, valinit[0]]
714714
handleXY_2 = [.5, valinit[1]]
715715
else:
@@ -719,9 +719,15 @@ def __init__(
719719
facecolor=track_color
720720
)
721721
ax.add_patch(self.track)
722-
self.poly = ax.axvspan(valinit[0], valinit[1], 0, 1, **kwargs)
722+
poly_transform = self.ax.get_xaxis_transform(which="grid")
723723
handleXY_1 = [valinit[0], .5]
724724
handleXY_2 = [valinit[1], .5]
725+
self.poly = Polygon(np.zeros([5, 2]), **kwargs)
726+
self._update_selection_poly(*valinit)
727+
self.poly.set_transform(poly_transform)
728+
self.poly.get_path()._interpolation_steps = 100
729+
self.ax.add_patch(self.poly)
730+
self.ax._request_autoscale_view()
725731
self._handles = [
726732
ax.plot(
727733
*handleXY_1,
@@ -777,6 +783,27 @@ def __init__(
777783
self._active_handle = None
778784
self.set_val(valinit)
779785

786+
def _update_selection_poly(self, vmin, vmax):
787+
"""
788+
Update the vertices of the *self.poly* slider in-place
789+
to cover the data range *vmin*, *vmax*.
790+
"""
791+
# The vertices are positioned
792+
# 1 ------ 2
793+
# | |
794+
# 0, 4 ---- 3
795+
verts = self.poly.xy
796+
if self.orientation == "vertical":
797+
verts[0] = verts[4] = .25, vmin
798+
verts[1] = .25, vmax
799+
verts[2] = .75, vmax
800+
verts[3] = .75, vmin
801+
else:
802+
verts[0] = verts[4] = vmin, .25
803+
verts[1] = vmin, .75
804+
verts[2] = vmax, .75
805+
verts[3] = vmax, .25
806+
780807
def _min_in_bounds(self, min):
781808
"""Ensure the new min value is between valmin and self.val[1]."""
782809
if min <= self.valmin:
@@ -903,36 +930,24 @@ def set_val(self, val):
903930
"""
904931
val = np.sort(val)
905932
_api.check_shape((2,), val=val)
906-
val[0] = self._min_in_bounds(val[0])
907-
val[1] = self._max_in_bounds(val[1])
908-
xy = self.poly.xy
933+
vmin, vmax = val
934+
vmin = self._min_in_bounds(vmin)
935+
vmax = self._max_in_bounds(vmax)
936+
self._update_selection_poly(vmin, vmax)
909937
if self.orientation == "vertical":
910-
xy[0] = .25, val[0]
911-
xy[1] = .25, val[1]
912-
xy[2] = .75, val[1]
913-
xy[3] = .75, val[0]
914-
xy[4] = .25, val[0]
915-
916-
self._handles[0].set_ydata([val[0]])
917-
self._handles[1].set_ydata([val[1]])
938+
self._handles[0].set_ydata([vmin])
939+
self._handles[1].set_ydata([vmax])
918940
else:
919-
xy[0] = val[0], .25
920-
xy[1] = val[0], .75
921-
xy[2] = val[1], .75
922-
xy[3] = val[1], .25
923-
xy[4] = val[0], .25
941+
self._handles[0].set_xdata([vmin])
942+
self._handles[1].set_xdata([vmax])
924943

925-
self._handles[0].set_xdata([val[0]])
926-
self._handles[1].set_xdata([val[1]])
927-
928-
self.poly.xy = xy
929-
self.valtext.set_text(self._format(val))
944+
self.valtext.set_text(self._format((vmin, vmax)))
930945

931946
if self.drawon:
932947
self.ax.figure.canvas.draw_idle()
933-
self.val = val
948+
self.val = (vmin, vmax)
934949
if self.eventson:
935-
self._observers.process("changed", val)
950+
self._observers.process("changed", (vmin, vmax))
936951

937952
def on_changed(self, func):
938953
"""

0 commit comments

Comments
 (0)