Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,15 +1758,22 @@ def test_polygon_selector_box_props_handle_props(ax):
def test_polygon_selector_clear_method(ax):
onselect = mock.Mock(spec=noop, return_value=None)
tool = widgets.PolygonSelector(ax, onselect)
artist = tool._selection_artist

tool.clear()
assert not tool._selection_completed
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
center = [((x0 + x1) / 2, (y0 + y1) / 2)]
# Cursor resets to center of the axes when last position is unknown.
np.testing.assert_equal(artist.get_xydata(), center)

for result in ([(50, 50), (150, 50), (50, 150), (50, 50)],
[(50, 50), (100, 50), (50, 150), (50, 50)]):
for xy in result:
for event in polygon_place_vertex(ax, xy):
event._process()

artist = tool._selection_artist

assert tool._selection_completed
assert tool.get_visible()
assert artist.get_visible()
Expand All @@ -1775,7 +1782,8 @@ def test_polygon_selector_clear_method(ax):

tool.clear()
assert not tool._selection_completed
np.testing.assert_equal(artist.get_xydata(), [(0, 0)])
# Cursor resets to last known position.
np.testing.assert_equal(artist.get_xydata(), [result[-1]])


@pytest.mark.parametrize("horizOn", [False, True])
Expand Down
11 changes: 10 additions & 1 deletion lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4368,7 +4368,16 @@ def verts(self, xys):

def _clear_without_update(self):
self._selection_completed = False
self._xys = [(0, 0)]
prev = self._prev_event
if (prev is not None
and prev.xdata is not None and prev.ydata is not None
and not np.isnan(prev.xdata) and not np.isnan(prev.ydata)):
# Reset the pending vertex to the last known cursor position.
self._xys = [(prev.xdata, prev.ydata)]
else:
x0, x1 = self.ax.get_xlim()
y0, y1 = self.ax.get_ylim()
self._xys = [((x0 + x1) / 2, (y0 + y1) / 2)]
self._draw_polygon_without_update()


Expand Down
Loading