Skip to content

Commit 2526b81

Browse files
thiagoluisbeckerQuLogic
authored andcommitted
Make onselect argument to selector widget optional
1 parent 9675122 commit 2526b81

File tree

4 files changed

+48
-44
lines changed

4 files changed

+48
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
onselect argument to selector widgets made optional
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The *onselect* argument to `.EllipseSelector`, `.LassoSelector`, `.PolygonSelector`, and
5+
`.RectangleSelector` is no longer required.

lib/matplotlib/tests/test_widgets.py

+30-35
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_save_blitted_widget_as_pdf():
7171
def test_rectangle_selector(ax, kwargs):
7272
onselect = mock.Mock(spec=noop, return_value=None)
7373

74-
tool = widgets.RectangleSelector(ax, onselect, **kwargs)
74+
tool = widgets.RectangleSelector(ax, onselect=onselect, **kwargs)
7575
do_event(tool, 'press', xdata=100, ydata=100, button=1)
7676
do_event(tool, 'onmove', xdata=199, ydata=199, button=1)
7777

@@ -105,7 +105,7 @@ def test_rectangle_minspan(ax, spancoords, minspanx, x1, minspany, y1):
105105
minspanx, minspany = (ax.transData.transform((x1, y1)) -
106106
ax.transData.transform((x0, y0)))
107107

108-
tool = widgets.RectangleSelector(ax, onselect, interactive=True,
108+
tool = widgets.RectangleSelector(ax, onselect=onselect, interactive=True,
109109
spancoords=spancoords,
110110
minspanx=minspanx, minspany=minspany)
111111
# Too small to create a selector
@@ -132,7 +132,7 @@ def test_rectangle_minspan(ax, spancoords, minspanx, x1, minspany, y1):
132132

133133

134134
def test_deprecation_selector_visible_attribute(ax):
135-
tool = widgets.RectangleSelector(ax, lambda *args: None)
135+
tool = widgets.RectangleSelector(ax)
136136

137137
assert tool.get_visible()
138138

@@ -145,7 +145,7 @@ def test_deprecation_selector_visible_attribute(ax):
145145
[[True, (60, 75)],
146146
[False, (30, 20)]])
147147
def test_rectangle_drag(ax, drag_from_anywhere, new_center):
148-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True,
148+
tool = widgets.RectangleSelector(ax, interactive=True,
149149
drag_from_anywhere=drag_from_anywhere)
150150
# Create rectangle
151151
click_and_drag(tool, start=(0, 10), end=(100, 120))
@@ -166,7 +166,7 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
166166

167167

168168
def test_rectangle_selector_set_props_handle_props(ax):
169-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True,
169+
tool = widgets.RectangleSelector(ax, interactive=True,
170170
props=dict(facecolor='b', alpha=0.2),
171171
handle_props=dict(alpha=0.5))
172172
# Create rectangle
@@ -187,7 +187,7 @@ def test_rectangle_selector_set_props_handle_props(ax):
187187

188188

189189
def test_rectangle_resize(ax):
190-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
190+
tool = widgets.RectangleSelector(ax, interactive=True)
191191
# Create rectangle
192192
click_and_drag(tool, start=(0, 10), end=(100, 120))
193193
assert tool.extents == (0.0, 100.0, 10.0, 120.0)
@@ -222,7 +222,7 @@ def test_rectangle_resize(ax):
222222

223223

224224
def test_rectangle_add_state(ax):
225-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
225+
tool = widgets.RectangleSelector(ax, interactive=True)
226226
# Create rectangle
227227
click_and_drag(tool, start=(70, 65), end=(125, 130))
228228

@@ -238,7 +238,7 @@ def test_rectangle_add_state(ax):
238238

239239
@pytest.mark.parametrize('add_state', [True, False])
240240
def test_rectangle_resize_center(ax, add_state):
241-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
241+
tool = widgets.RectangleSelector(ax, interactive=True)
242242
# Create rectangle
243243
click_and_drag(tool, start=(70, 65), end=(125, 130))
244244
assert tool.extents == (70.0, 125.0, 65.0, 130.0)
@@ -312,7 +312,7 @@ def test_rectangle_resize_center(ax, add_state):
312312

313313
@pytest.mark.parametrize('add_state', [True, False])
314314
def test_rectangle_resize_square(ax, add_state):
315-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
315+
tool = widgets.RectangleSelector(ax, interactive=True)
316316
# Create rectangle
317317
click_and_drag(tool, start=(70, 65), end=(120, 115))
318318
assert tool.extents == (70.0, 120.0, 65.0, 115.0)
@@ -385,7 +385,7 @@ def test_rectangle_resize_square(ax, add_state):
385385

386386

387387
def test_rectangle_resize_square_center(ax):
388-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
388+
tool = widgets.RectangleSelector(ax, interactive=True)
389389
# Create rectangle
390390
click_and_drag(tool, start=(70, 65), end=(120, 115))
391391
tool.add_state('square')
@@ -450,7 +450,7 @@ def test_rectangle_resize_square_center(ax):
450450
@pytest.mark.parametrize('selector_class',
451451
[widgets.RectangleSelector, widgets.EllipseSelector])
452452
def test_rectangle_rotate(ax, selector_class):
453-
tool = selector_class(ax, onselect=noop, interactive=True)
453+
tool = selector_class(ax, interactive=True)
454454
# Draw rectangle
455455
click_and_drag(tool, start=(100, 100), end=(130, 140))
456456
assert tool.extents == (100, 130, 100, 140)
@@ -483,7 +483,7 @@ def test_rectangle_rotate(ax, selector_class):
483483

484484

485485
def test_rectangle_add_remove_set(ax):
486-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
486+
tool = widgets.RectangleSelector(ax, interactive=True)
487487
# Draw rectangle
488488
click_and_drag(tool, start=(100, 100), end=(130, 140))
489489
assert tool.extents == (100, 130, 100, 140)
@@ -499,7 +499,7 @@ def test_rectangle_add_remove_set(ax):
499499
def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
500500
ax.set_aspect(0.8)
501501

502-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True,
502+
tool = widgets.RectangleSelector(ax, interactive=True,
503503
use_data_coordinates=use_data_coordinates)
504504
# Create rectangle
505505
click_and_drag(tool, start=(70, 65), end=(120, 115))
@@ -531,8 +531,7 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
531531

532532
def test_ellipse(ax):
533533
"""For ellipse, test out the key modifiers"""
534-
tool = widgets.EllipseSelector(ax, onselect=noop,
535-
grab_range=10, interactive=True)
534+
tool = widgets.EllipseSelector(ax, grab_range=10, interactive=True)
536535
tool.extents = (100, 150, 100, 150)
537536

538537
# drag the rectangle
@@ -558,9 +557,7 @@ def test_ellipse(ax):
558557

559558

560559
def test_rectangle_handles(ax):
561-
tool = widgets.RectangleSelector(ax, onselect=noop,
562-
grab_range=10,
563-
interactive=True,
560+
tool = widgets.RectangleSelector(ax, grab_range=10, interactive=True,
564561
handle_props={'markerfacecolor': 'r',
565562
'markeredgecolor': 'b'})
566563
tool.extents = (100, 150, 100, 150)
@@ -595,7 +592,7 @@ def test_rectangle_selector_onselect(ax, interactive):
595592
# check when press and release events take place at the same position
596593
onselect = mock.Mock(spec=noop, return_value=None)
597594

598-
tool = widgets.RectangleSelector(ax, onselect, interactive=interactive)
595+
tool = widgets.RectangleSelector(ax, onselect=onselect, interactive=interactive)
599596
# move outside of axis
600597
click_and_drag(tool, start=(100, 110), end=(150, 120))
601598

@@ -611,7 +608,7 @@ def test_rectangle_selector_onselect(ax, interactive):
611608
def test_rectangle_selector_ignore_outside(ax, ignore_event_outside):
612609
onselect = mock.Mock(spec=noop, return_value=None)
613610

614-
tool = widgets.RectangleSelector(ax, onselect,
611+
tool = widgets.RectangleSelector(ax, onselect=onselect,
615612
ignore_event_outside=ignore_event_outside)
616613
click_and_drag(tool, start=(100, 110), end=(150, 120))
617614
onselect.assert_called_once()
@@ -773,10 +770,11 @@ def test_span_selector_set_props_handle_props(ax):
773770

774771
@pytest.mark.parametrize('selector', ['span', 'rectangle'])
775772
def test_selector_clear(ax, selector):
776-
kwargs = dict(ax=ax, onselect=noop, interactive=True)
773+
kwargs = dict(ax=ax, interactive=True)
777774
if selector == 'span':
778775
Selector = widgets.SpanSelector
779776
kwargs['direction'] = 'horizontal'
777+
kwargs['onselect'] = noop
780778
else:
781779
Selector = widgets.RectangleSelector
782780

@@ -807,7 +805,7 @@ def test_selector_clear_method(ax, selector):
807805
interactive=True,
808806
ignore_event_outside=True)
809807
else:
810-
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
808+
tool = widgets.RectangleSelector(ax, interactive=True)
811809
click_and_drag(tool, start=(10, 10), end=(100, 120))
812810
assert tool._selection_completed
813811
assert tool.get_visible()
@@ -1000,7 +998,7 @@ def test_span_selector_extents(ax):
1000998
def test_lasso_selector(ax, kwargs):
1001999
onselect = mock.Mock(spec=noop, return_value=None)
10021000

1003-
tool = widgets.LassoSelector(ax, onselect, **kwargs)
1001+
tool = widgets.LassoSelector(ax, onselect=onselect, **kwargs)
10041002
do_event(tool, 'press', xdata=100, ydata=100, button=1)
10051003
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
10061004
do_event(tool, 'release', xdata=150, ydata=150, button=1)
@@ -1011,7 +1009,8 @@ def test_lasso_selector(ax, kwargs):
10111009
def test_lasso_selector_set_props(ax):
10121010
onselect = mock.Mock(spec=noop, return_value=None)
10131011

1014-
tool = widgets.LassoSelector(ax, onselect, props=dict(color='b', alpha=0.2))
1012+
tool = widgets.LassoSelector(ax, onselect=onselect,
1013+
props=dict(color='b', alpha=0.2))
10151014

10161015
artist = tool._selection_artist
10171016
assert mcolors.same_color(artist.get_color(), 'b')
@@ -1380,7 +1379,7 @@ def check_polygon_selector(event_sequence, expected_result, selections_count,
13801379

13811380
onselect = mock.Mock(spec=noop, return_value=None)
13821381

1383-
tool = widgets.PolygonSelector(ax, onselect, **kwargs)
1382+
tool = widgets.PolygonSelector(ax, onselect=onselect, **kwargs)
13841383

13851384
for (etype, event_args) in event_sequence:
13861385
do_event(tool, etype, **event_args)
@@ -1517,7 +1516,7 @@ def test_polygon_selector(draw_bounding_box):
15171516

15181517
@pytest.mark.parametrize('draw_bounding_box', [False, True])
15191518
def test_polygon_selector_set_props_handle_props(ax, draw_bounding_box):
1520-
tool = widgets.PolygonSelector(ax, onselect=noop,
1519+
tool = widgets.PolygonSelector(ax,
15211520
props=dict(color='b', alpha=0.2),
15221521
handle_props=dict(alpha=0.5),
15231522
draw_bounding_box=draw_bounding_box)
@@ -1554,8 +1553,7 @@ def test_rect_visibility(fig_test, fig_ref):
15541553
ax_test = fig_test.subplots()
15551554
_ = fig_ref.subplots()
15561555

1557-
tool = widgets.RectangleSelector(ax_test, onselect=noop,
1558-
props={'visible': False})
1556+
tool = widgets.RectangleSelector(ax_test, props={'visible': False})
15591557
tool.extents = (0.2, 0.8, 0.3, 0.7)
15601558

15611559

@@ -1608,8 +1606,7 @@ def test_polygon_selector_redraw(ax, draw_bounding_box):
16081606
*polygon_place_vertex(*verts[1]),
16091607
]
16101608

1611-
tool = widgets.PolygonSelector(ax, onselect=noop,
1612-
draw_bounding_box=draw_bounding_box)
1609+
tool = widgets.PolygonSelector(ax, draw_bounding_box=draw_bounding_box)
16131610
for (etype, event_args) in event_sequence:
16141611
do_event(tool, etype, **event_args)
16151612
# After removing two verts, only one remains, and the
@@ -1623,14 +1620,12 @@ def test_polygon_selector_verts_setter(fig_test, fig_ref, draw_bounding_box):
16231620
verts = [(0.1, 0.4), (0.5, 0.9), (0.3, 0.2)]
16241621
ax_test = fig_test.add_subplot()
16251622

1626-
tool_test = widgets.PolygonSelector(
1627-
ax_test, onselect=noop, draw_bounding_box=draw_bounding_box)
1623+
tool_test = widgets.PolygonSelector(ax_test, draw_bounding_box=draw_bounding_box)
16281624
tool_test.verts = verts
16291625
assert tool_test.verts == verts
16301626

16311627
ax_ref = fig_ref.add_subplot()
1632-
tool_ref = widgets.PolygonSelector(
1633-
ax_ref, onselect=noop, draw_bounding_box=draw_bounding_box)
1628+
tool_ref = widgets.PolygonSelector(ax_ref, draw_bounding_box=draw_bounding_box)
16341629
event_sequence = [
16351630
*polygon_place_vertex(*verts[0]),
16361631
*polygon_place_vertex(*verts[1]),
@@ -1654,7 +1649,7 @@ def test_polygon_selector_box(ax):
16541649
]
16551650

16561651
# Create selector
1657-
tool = widgets.PolygonSelector(ax, onselect=noop, draw_bounding_box=True)
1652+
tool = widgets.PolygonSelector(ax, draw_bounding_box=True)
16581653
for (etype, event_args) in event_sequence:
16591654
do_event(tool, etype, **event_args)
16601655

lib/matplotlib/widgets.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -2091,12 +2091,15 @@ def onmove(self, event):
20912091

20922092
class _SelectorWidget(AxesWidget):
20932093

2094-
def __init__(self, ax, onselect, useblit=False, button=None,
2094+
def __init__(self, ax, onselect=None, useblit=False, button=None,
20952095
state_modifier_keys=None, use_data_coordinates=False):
20962096
super().__init__(ax)
20972097

20982098
self._visible = True
2099-
self.onselect = onselect
2099+
if onselect is None:
2100+
self.onselect = lambda *args: None
2101+
else:
2102+
self.onselect = onselect
21002103
self.useblit = useblit and self.canvas.supports_blit
21012104
self.connect_default_events()
21022105

@@ -3154,7 +3157,8 @@ class RectangleSelector(_SelectorWidget):
31543157
See also: :doc:`/gallery/widgets/rectangle_selector`
31553158
"""
31563159

3157-
def __init__(self, ax, onselect, *, minspanx=0, minspany=0, useblit=False,
3160+
def __init__(self, ax, onselect=None, *, minspanx=0,
3161+
minspany=0, useblit=False,
31583162
props=None, spancoords='data', button=None, grab_range=10,
31593163
handle_props=None, interactive=False,
31603164
state_modifier_keys=None, drag_from_anywhere=False,
@@ -3691,7 +3695,7 @@ def onselect(verts):
36913695
which corresponds to all buttons.
36923696
"""
36933697

3694-
def __init__(self, ax, onselect, *, useblit=True, props=None, button=None):
3698+
def __init__(self, ax, onselect=None, *, useblit=True, props=None, button=None):
36953699
super().__init__(ax, onselect, useblit=useblit, button=button)
36963700
self.verts = None
36973701
props = {
@@ -3801,7 +3805,7 @@ class PolygonSelector(_SelectorWidget):
38013805
point.
38023806
"""
38033807

3804-
def __init__(self, ax, onselect, *, useblit=False,
3808+
def __init__(self, ax, onselect=None, *, useblit=False,
38053809
props=None, handle_props=None, grab_range=10,
38063810
draw_bounding_box=False, box_handle_props=None,
38073811
box_props=None):

lib/matplotlib/widgets.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class _SelectorWidget(AxesWidget):
276276
def __init__(
277277
self,
278278
ax: Axes,
279-
onselect: Callable[[float, float], Any],
279+
onselect: Callable[[float, float], Any] | None = ...,
280280
useblit: bool = ...,
281281
button: MouseButton | Collection[MouseButton] | None = ...,
282282
state_modifier_keys: dict[str, str] | None = ...,
@@ -403,7 +403,7 @@ class RectangleSelector(_SelectorWidget):
403403
def __init__(
404404
self,
405405
ax: Axes,
406-
onselect: Callable[[MouseEvent, MouseEvent], Any],
406+
onselect: Callable[[MouseEvent, MouseEvent], Any] | None = ...,
407407
*,
408408
minspanx: float = ...,
409409
minspany: float = ...,
@@ -443,7 +443,7 @@ class LassoSelector(_SelectorWidget):
443443
def __init__(
444444
self,
445445
ax: Axes,
446-
onselect: Callable[[list[tuple[float, float]]], Any],
446+
onselect: Callable[[list[tuple[float, float]]], Any] | None = ...,
447447
*,
448448
useblit: bool = ...,
449449
props: dict[str, Any] | None = ...,
@@ -455,7 +455,7 @@ class PolygonSelector(_SelectorWidget):
455455
def __init__(
456456
self,
457457
ax: Axes,
458-
onselect: Callable[[ArrayLike, ArrayLike], Any],
458+
onselect: Callable[[ArrayLike, ArrayLike], Any] | None = ...,
459459
*,
460460
useblit: bool = ...,
461461
props: dict[str, Any] | None = ...,

0 commit comments

Comments
 (0)