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

Skip to content

Commit ae93c40

Browse files
committed
Fix docstring minspan* of selectors
Improve docstring of selector: explain press and release event at same coordinates clear the selector Clarify the docstring about when onselect is called and minspan* and fix calling onselect for RectangleSelector. Rename `self._completed` to `self._selection_completed` in Selectors. Improve `onselect` and `minspan` interaction for the `SpanSelector`. Add next_whats_new entry Add ignore_event_outside option for RectangleSelector and EllipseSelector Add ignore_event_outside option for SpanSelector Fix docstring for minspan argument of selectors. Simplify RectangleSelector `_release` and test to check that `onselect` is called when the press and release event are the same Fix onselect call when press and release events are identical than minspan
1 parent 27ab013 commit ae93c40

File tree

3 files changed

+226
-40
lines changed

3 files changed

+226
-40
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Ignore events outside selection
2+
-------------------------------
3+
The `~matplotlib.widgets.SpanSelector`, `~matplotlib.widgets.RectangleSelector`
4+
and `~matplotlib.widgets.EllipseSelector` have a new keyword argument,
5+
*ignore_event_outside*, which when set to `True` will ignore events outside of
6+
the current selection. The handles or the new dragging functionality can instead
7+
be used to change the selection.

lib/matplotlib/tests/test_widgets.py

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,63 @@ def onselect(epress, erelease):
190190
tool._corner_handles.artist.get_markeredgecolor(), 'b')
191191

192192

193+
@pytest.mark.parametrize('interactive', [True, False])
194+
def test_rectangle_selector_onselect(interactive):
195+
# check when press and release events take place at the same position
196+
ax = get_ax()
197+
198+
def onselect(vmin, vmax):
199+
ax._got_onselect = True
200+
201+
tool = widgets.RectangleSelector(ax, onselect, interactive=interactive)
202+
do_event(tool, 'press', xdata=100, ydata=110, button=1)
203+
# move outside of axis
204+
do_event(tool, 'onmove', xdata=150, ydata=120, button=1)
205+
do_event(tool, 'release', xdata=150, ydata=120, button=1)
206+
207+
assert tool.ax._got_onselect
208+
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
209+
210+
# Reset tool.ax._got_onselect
211+
tool.ax._got_onselect = False
212+
213+
do_event(tool, 'press', xdata=10, ydata=100, button=1)
214+
do_event(tool, 'release', xdata=10, ydata=100, button=1)
215+
216+
assert tool.ax._got_onselect
217+
218+
219+
@pytest.mark.parametrize('ignore_event_outside', [True, False])
220+
def test_rectangle_selector_ignore_outside(ignore_event_outside):
221+
ax = get_ax()
222+
def onselect(vmin, vmax):
223+
ax._got_onselect = True
224+
225+
tool = widgets.RectangleSelector(ax, onselect,
226+
ignore_event_outside=ignore_event_outside)
227+
do_event(tool, 'press', xdata=100, ydata=110, button=1)
228+
do_event(tool, 'onmove', xdata=150, ydata=120, button=1)
229+
do_event(tool, 'release', xdata=150, ydata=120, button=1)
230+
231+
assert tool.ax._got_onselect
232+
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
233+
234+
# Reset
235+
ax._got_onselect = False
236+
# Trigger event outside of span
237+
do_event(tool, 'press', xdata=150, ydata=150, button=1)
238+
do_event(tool, 'onmove', xdata=160, ydata=160, button=1)
239+
do_event(tool, 'release', xdata=160, ydata=160, button=1)
240+
if ignore_event_outside:
241+
# event have been ignored and span haven't changed.
242+
assert not ax._got_onselect
243+
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
244+
else:
245+
# A new shape is created
246+
assert ax._got_onselect
247+
assert tool.extents == (150.0, 160.0, 150.0, 160.0)
248+
249+
193250
def check_span(*args, **kwargs):
194251
ax = get_ax()
195252

@@ -222,13 +279,79 @@ def test_span_selector():
222279
check_span('horizontal', minspan=10, useblit=True)
223280
check_span('vertical', onmove_callback=True, button=1)
224281
check_span('horizontal', props=dict(fill=True))
282+
check_span('horizontal', interactive=True)
283+
284+
285+
@pytest.mark.parametrize('interactive', [True, False])
286+
def test_span_selector_onselect(interactive):
287+
# check when press and release events take place at the same position
288+
ax = get_ax()
289+
290+
def onselect(vmin, vmax):
291+
ax._got_onselect = True
292+
293+
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
294+
interactive=interactive)
295+
do_event(tool, 'press', xdata=100, ydata=100, button=1)
296+
# move outside of axis
297+
do_event(tool, 'onmove', xdata=150, ydata=100, button=1)
298+
do_event(tool, 'release', xdata=150, ydata=100, button=1)
299+
300+
assert tool.ax._got_onselect
301+
assert tool.extents == (100, 150)
302+
303+
# Reset tool.ax._got_onselect
304+
tool.ax._got_onselect = False
305+
306+
do_event(tool, 'press', xdata=10, ydata=100, button=1)
307+
do_event(tool, 'release', xdata=10, ydata=100, button=1)
308+
309+
assert tool.ax._got_onselect
310+
311+
312+
@pytest.mark.parametrize('ignore_event_outside', [True, False])
313+
def test_span_selector_ignore_outside(ignore_event_outside):
314+
ax = get_ax()
315+
def onselect(vmin, vmax):
316+
ax._got_onselect = True
317+
318+
def onmove(vmin, vmax):
319+
ax._got_on_move = True
320+
321+
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
322+
onmove_callback=onmove,
323+
ignore_event_outside=ignore_event_outside)
324+
do_event(tool, 'press', xdata=100, ydata=100, button=1)
325+
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
326+
do_event(tool, 'release', xdata=125, ydata=125, button=1)
327+
assert ax._got_onselect
328+
assert ax._got_on_move
329+
assert tool.extents == (100, 125)
330+
331+
# Reset
332+
ax._got_onselect = False
333+
ax._got_on_move = False
334+
# Trigger event outside of span
335+
do_event(tool, 'press', xdata=150, ydata=150, button=1)
336+
do_event(tool, 'onmove', xdata=160, ydata=160, button=1)
337+
do_event(tool, 'release', xdata=160, ydata=160, button=1)
338+
if ignore_event_outside:
339+
# event have been ignored and span haven't changed.
340+
assert not ax._got_onselect
341+
assert not ax._got_on_move
342+
assert tool.extents == (100, 125)
343+
else:
344+
# A new shape is created
345+
assert ax._got_onselect
346+
assert ax._got_on_move
347+
assert tool.extents == (150, 160)
225348

226349

227350
@pytest.mark.parametrize('drag_from_anywhere', [True, False])
228351
def test_span_selector_drag(drag_from_anywhere):
229352
ax = get_ax()
230353

231-
def onselect(epress, erelease):
354+
def onselect(*args):
232355
pass
233356

234357
# Create span
@@ -263,7 +386,7 @@ def onselect(epress, erelease):
263386
def test_span_selector_direction():
264387
ax = get_ax()
265388

266-
def onselect(epress, erelease):
389+
def onselect(*args):
267390
pass
268391

269392
tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True)
@@ -702,7 +825,7 @@ def test_MultiCursor(horizOn, vertOn):
702825
def test_rect_visibility(fig_test, fig_ref):
703826
# Check that requesting an invisible selector makes it invisible
704827
ax_test = fig_test.subplots()
705-
ax_ref = fig_ref.subplots()
828+
_ = fig_ref.subplots()
706829

707830
def onselect(verts):
708831
pass

0 commit comments

Comments
 (0)