-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Lasso selector #730
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
Lasso selector #730
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8af797d
Change cursors and selectors to subclass Widget.
tonysyu 5b35427
Remove call to `new_axes` in SpanSelector.
tonysyu e37c135
Add AxesWidget class and let it initialize `ax` and `canvas` attributes.
tonysyu 21bca27
Add `connect_event` and `disconnect_events` to AxesWidget.
tonysyu c8f152a
Add default ignore method and check it in callbacks.
tonysyu 6baa65a
Fix: save correct callback ids.
tonysyu 0f0286e
Remove duplicate code in `SpanSelector.__init__`.
tonysyu 8345d4c
Add LassoSelector widget with demo.
tonysyu adb1754
DOC: Fix sphinx class references.
tonysyu f169460
Fix and improve docstring.
tonysyu c668798
Merge branch 'base-widget' into lasso-selector
tonysyu fe3e2b8
Use `Path.contains_point` instead of `nxutils` function.
tonysyu 78798f1
Add docstrings for `Lasso` and `LassoSelector`.
tonysyu 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
Add LassoSelector widget with demo.
Note: I put the demo in the "widgets" directory even though the `Lasso` demo is in "event_handling".
- Loading branch information
commit 8345d4c6afcd8afa9d646d554440de27fcc7d49c
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,81 @@ | ||
import numpy as np | ||
|
||
from matplotlib.widgets import LassoSelector | ||
from matplotlib.nxutils import points_inside_poly | ||
|
||
|
||
class SelectFromCollection(object): | ||
"""Select indices from a matplotlib collection using :class:`LassoSelector`. | ||
|
||
Selected indices are saved in the `ind` attribute. This tool highlights | ||
selected points by fading them out (i.e., reducing their alpha values). | ||
If your collection has alpha < 1, this tool will permanently alter them. | ||
|
||
Note that this tool selects collection objects based on their *origins* | ||
(i.e., `offsets`). | ||
|
||
Parameters | ||
---------- | ||
ax : :class:`Axes` | ||
Axes to interact with. | ||
|
||
collection : :class:`Collection` | ||
Collection you want to select from. | ||
|
||
alpha_other : 0 <= float <= 1 | ||
To highlight a selection, this tool sets all selected points to an | ||
alpha value of 1 and non-selected points to `alpha_other`. | ||
""" | ||
def __init__(self, ax, collection, alpha_other=0.3): | ||
self.canvas = ax.figure.canvas | ||
self.collection = collection | ||
self.alpha_other = alpha_other | ||
|
||
self.xys = collection.get_offsets() | ||
self.Npts = len(self.xys) | ||
|
||
# Ensure that we have separate colors for each object | ||
self.fc = collection.get_facecolors() | ||
if len(self.fc) == 0: | ||
raise ValueError('Collection must have a facecolor') | ||
elif len(self.fc) == 1: | ||
self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1) | ||
|
||
self.lasso = LassoSelector(ax, onselect=self.onselect) | ||
self.ind = [] | ||
|
||
def onselect(self, verts): | ||
self.ind = np.nonzero(points_inside_poly(self.xys, verts))[0] | ||
self.fc[:, -1] = self.alpha_other | ||
self.fc[self.ind, -1] = 1 | ||
self.collection.set_facecolors(self.fc) | ||
self.canvas.draw_idle() | ||
|
||
def disconnect(self): | ||
self.lasso.disconnect_events() | ||
self.fc[:, -1] = 1 | ||
self.collection.set_facecolors(self.fc) | ||
self.canvas.draw_idle() | ||
|
||
|
||
if __name__ == '__main__': | ||
import matplotlib.pyplot as plt | ||
|
||
plt.ion() | ||
data = np.random.rand(100, 2) | ||
|
||
subplot_kw = dict(xlim=(0,1), ylim=(0,1), autoscale_on=False) | ||
fig, ax = plt.subplots(subplot_kw=subplot_kw) | ||
|
||
pts = ax.scatter(data[:, 0], data[:, 1], s=80) | ||
selector = SelectFromCollection(ax, pts) | ||
|
||
plt.draw() | ||
raw_input('Press any key to accept selected points') | ||
print "Selected points:" | ||
print selector.xys[selector.ind] | ||
selector.disconnect() | ||
|
||
# Block end of script so you can check that lasso is disconnected. | ||
raw_input('Press any key to quit') | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That can't work since
nxutils
was removed in master... see #732 for example of how to get around that one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'm on the latest master, and it works fine on my system. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually,... I didn't clean out my build, so I had a leftover
nxutils.so
. I just converted to usePath.contains_point
. Thanks for the tip!