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

Skip to content

Commit 92d1705

Browse files
committed
Use the property setter of direction when initialising a SpanSelector and add test.
1 parent e280ed9 commit 92d1705

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,27 @@ def onselect(epress, erelease):
259259
assert tool.extents == (175, 185)
260260

261261

262+
def test_span_selector_direction():
263+
ax = get_ax()
264+
265+
def onselect(epress, erelease):
266+
pass
267+
268+
tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True)
269+
assert tool.direction == 'horizontal'
270+
assert tool._edge_handles.direction == 'horizontal'
271+
272+
with pytest.raises(ValueError):
273+
tool = widgets.SpanSelector(ax, onselect, 'invalid_direction')
274+
275+
tool.direction = 'vertical'
276+
assert tool.direction == 'vertical'
277+
assert tool._edge_handles.direction == 'vertical'
278+
279+
with pytest.raises(ValueError):
280+
tool.direction = 'invalid_string'
281+
282+
262283
def test_tool_line_handle():
263284
ax = get_ax()
264285

lib/matplotlib/widgets.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ def __init__(self, ax, onselect, direction, minspan=0, useblit=False,
19901990

19911991
rectprops['animated'] = self.useblit
19921992

1993-
self._direction = direction
1993+
self.direction = direction
19941994

19951995
self._rect = None
19961996
self.visible = True
@@ -2114,7 +2114,7 @@ def direction(self):
21142114
def direction(self, direction):
21152115
"""Set the direction of the span selector."""
21162116
_api.check_in_list(['horizontal', 'vertical'], direction=direction)
2117-
if direction != self._direction:
2117+
if hasattr(self, '_direction') and direction != self._direction:
21182118
# remove previous artists
21192119
self._rect.remove()
21202120
if self._interactive:
@@ -2125,6 +2125,8 @@ def direction(self, direction):
21252125
self.new_axes(self.ax)
21262126
if self._interactive:
21272127
self._setup_edge_handle(self._edge_handles._line_props)
2128+
else:
2129+
self._direction = direction
21282130

21292131
def _release(self, event):
21302132
"""Button release event handler."""

0 commit comments

Comments
 (0)