-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Allow PolygonSelector points to be removed #19660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Removing points on a PolygonSelector | ||
------------------------------------ | ||
After completing a `~matplotlib.widgets.PolygonSelector`, individual | ||
points can now be removed by right-clicking on them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2759,11 +2759,17 @@ class PolygonSelector(_SelectorWidget): | |
Select a polygon region of an axes. | ||
|
||
Place vertices with each mouse click, and make the selection by completing | ||
the polygon (clicking on the first vertex). Hold the *ctrl* key and click | ||
and drag a vertex to reposition it (the *ctrl* key is not necessary if the | ||
polygon has already been completed). Hold the *shift* key and click and | ||
drag anywhere in the axes to move all vertices. Press the *esc* key to | ||
start a new polygon. | ||
the polygon (clicking on the first vertex). Once drawn individual vertices | ||
can be moved by clicking and dragging with the left mouse button, or | ||
removed by clicking the right mouse button. | ||
|
||
In addition, the following modifier keys can be used: | ||
|
||
- Hold *ctrl* and click and drag a vertex to reposition it before the | ||
polygon has been completed. | ||
- Hold the *shift* key and click and drag anywhere in the axes to move | ||
all vertices. | ||
- Press the *esc* key to start a new polygon. | ||
|
||
For the selector to remain responsive you must keep a reference to it. | ||
|
||
|
@@ -2789,6 +2795,12 @@ class PolygonSelector(_SelectorWidget): | |
Examples | ||
-------- | ||
:doc:`/gallery/widgets/polygon_selector_demo` | ||
|
||
Notes | ||
----- | ||
If only one point remains after removing points, the selector reverts to an | ||
incomplete state and you can start drawing a new polygon from the existing | ||
point. | ||
""" | ||
|
||
def __init__(self, ax, onselect, useblit=False, | ||
|
@@ -2827,6 +2839,33 @@ def __init__(self, ax, onselect, useblit=False, | |
self.artists = [self.line, self._polygon_handles.artist] | ||
self.set_visible(True) | ||
|
||
@property | ||
def _nverts(self): | ||
return len(self._xs) | ||
|
||
def _remove_vertex(self, i): | ||
"""Remove vertex with index i.""" | ||
if (self._nverts > 2 and | ||
self._polygon_completed and | ||
i in (0, self._nverts - 1)): | ||
# If selecting the first or final vertex, remove both first and | ||
# last vertex as they are the same for a closed polygon | ||
self._xs.pop(0) | ||
self._ys.pop(0) | ||
self._xs.pop(-1) | ||
self._ys.pop(-1) | ||
# Close the polygon again by appending the new first vertex to the | ||
# end | ||
self._xs.append(self._xs[0]) | ||
self._ys.append(self._ys[0]) | ||
else: | ||
self._xs.pop(i) | ||
self._ys.pop(i) | ||
if self._nverts <= 2: | ||
# If only one point left, return to incomplete state to let user | ||
# start drawing again | ||
self._polygon_completed = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add a test for this. |
||
|
||
def _press(self, event): | ||
"""Button press event handler.""" | ||
# Check for selection of a tool handle. | ||
|
@@ -2843,6 +2882,9 @@ def _release(self, event): | |
"""Button release event handler.""" | ||
# Release active tool handle. | ||
if self._active_handle_idx >= 0: | ||
if event.button == 3: | ||
self._remove_vertex(self._active_handle_idx) | ||
self._draw_polygon() | ||
self._active_handle_idx = -1 | ||
|
||
# Complete the polygon. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.