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

Skip to content

Commit e5a8a3a

Browse files
committed
Add clear method to selector
1 parent 96f6c90 commit e5a8a3a

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Clear selector
2+
--------------
3+
The selectors (`~matplotlib.widgets.SpanSelector`, `~matplotlib.widgets.RectangleSelector`,
4+
`~matplotlib.widgets.EllipseSelector`, `~matplotlib.widgets.PolygonSelector` and
5+
`~matplotlib.widgets.LassoSelector` have a new method *clear*, which will clear
6+
the current selection and get the selector ready to make a new selection. This
7+
is equivalent to press the *escape* key.

lib/matplotlib/tests/test_widgets.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,41 @@ def onselect(*args):
496496
assert not tool._selection_completed
497497

498498

499+
@pytest.mark.parametrize('selector', ['span', 'rectangle'])
500+
def test_selector_clear_method(selector):
501+
ax = get_ax()
502+
503+
def onselect(*args):
504+
pass
505+
506+
if selector == 'span':
507+
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
508+
interactive=True,
509+
ignore_event_outside=True)
510+
else:
511+
tool = widgets.RectangleSelector(ax, onselect, interactive=True)
512+
do_event(tool, 'press', xdata=10, ydata=10, button=1)
513+
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
514+
do_event(tool, 'release', xdata=100, ydata=120, button=1)
515+
assert tool._selection_completed
516+
assert tool.visible
517+
if selector == 'span':
518+
assert tool.extents == (10, 100)
519+
520+
tool.clear()
521+
assert not tool._selection_completed
522+
assert not tool.visible
523+
524+
# Do another cycle of events to make sure we can
525+
do_event(tool, 'press', xdata=10, ydata=10, button=1)
526+
do_event(tool, 'onmove', xdata=50, ydata=120, button=1)
527+
do_event(tool, 'release', xdata=50, ydata=120, button=1)
528+
assert tool._selection_completed
529+
assert tool.visible
530+
if selector == 'span':
531+
assert tool.extents == (10, 50)
532+
533+
499534
def test_tool_line_handle():
500535
ax = get_ax()
501536

lib/matplotlib/widgets.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,10 +1978,7 @@ def on_key_press(self, event):
19781978
key = event.key or ''
19791979
key = key.replace('ctrl', 'control')
19801980
if key == self.state_modifier_keys['clear']:
1981-
self._selection_completed = False
1982-
for artist in self.artists:
1983-
artist.set_visible(False)
1984-
self.update()
1981+
self.clear()
19851982
return
19861983
for (state, modifier) in self.state_modifier_keys.items():
19871984
if modifier in key:
@@ -2009,6 +2006,12 @@ def set_visible(self, visible):
20092006
for artist in self.artists:
20102007
artist.set_visible(visible)
20112008

2009+
def clear(self):
2010+
"""Clear the selection and set the selector ready to make a new one."""
2011+
self._selection_completed = False
2012+
self.set_visible(False)
2013+
self.update()
2014+
20122015
@property
20132016
def artists(self):
20142017
"""Tuple of the artists of the selector."""

0 commit comments

Comments
 (0)