8000 Merge pull request #23888 from lschr/fix_polygonselector_clear · matplotlib/matplotlib@98f36e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98f36e3

Browse files
authored
Merge pull request #23888 from lschr/fix_polygonselector_clear
Fix PolygonSelector.clear()
2 parents 8faa835 + a9c45c3 commit 98f36e3

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,29 @@ def test_polygon_selector_box(ax):
16861686
tool._box.extents, (20.0, 40.0, 30.0, 40.0))
16871687

16881688

1689+
def test_polygon_selector_clear_method(ax):
1690+
onselect = mock.Mock(spec=noop, return_value=None)
1691+
tool = widgets.PolygonSelector(ax, onselect)
1692+
1693+
for result in ([(50, 50), (150, 50), (50, 150), (50, 50)],
1694+
[(50, 50), (100, 50), (50, 150), (50, 50)]):
1695+
for x, y in result:
1696+
for etype, event_args in polygon_place_vertex(x, y):
1697+
do_event(tool, etype, **event_args)
1698+
1699+
artist = tool._selection_artist
1700+
1701+
assert tool._selection_completed
1702+
assert tool.get_visible()
1703+
assert artist.get_visible()
1704+
np.testing.assert_equal(artist.get_xydata(), result)
1705+
assert onselect.call_args == ((result[:-1],), {})
1706+
1707+
tool.clear()
1708+
assert not tool._selection_completed
1709+
np.testing.assert_equal(artist.get_xydata(), [(0, 0)])
1710+
1711+
16891712
@pytest.mark.parametrize("horizOn", [False, True])
16901713
@pytest.mark.parametrize("vertOn", [False, True])
16911714
def test_MultiCursor(horizOn, vertOn):

lib/matplotlib/widgets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4141,8 +4141,8 @@ def _on_key_release(self, event):
41414141
self._remove_box()
41424142
self.set_visible(True)
41434143

4144-
def _draw_polygon(self):
4145-
"""Redraw the polygon based on the new vertex positions."""
4144+
def _draw_polygon_without_update(self):
4145+
"""Redraw the polygon based on new vertex positions, no update()."""
41464146
xs, ys = zip(*self._xys) if self._xys else ([], [])
41474147
self._selection_artist.set_data(xs, ys)
41484148
self._update_box()
@@ -4155,6 +4155,10 @@ def _draw_polygon(self):
41554155
self._polygon_handles.set_data(xs[:-1], ys[:-1])
41564156
else:
41574157
self._polygon_handles.set_data(xs, ys)
4158+
4159+
def _draw_polygon(self):
4160+
"""Redraw the polygon based on the new vertex positions."""
4161+
self._draw_polygon_without_update()
41584162
self.update()
41594163

41604164
@property
@@ -4177,6 +4181,11 @@ def verts(self, xys):
41774181
self._add_box()
41784182
self._draw_polygon()
41794183

4184+
def _clear_without_update(self):
4185+
self._selection_completed = False
4186+
self._xys = [(0, 0)]
4187+
self._draw_polygon_without_update()
4188+
41804189

41814190
class Lasso(AxesWidget):
41824191
"""

0 commit comments

Comments
 (0)
0