New Feature - PolygonSelector Widget#8403
Conversation
phobson
left a comment
There was a problem hiding this comment.
This looks really cool! I haven't taken a deep look yet, but here are some comments that should get the CI builds to pass.
lib/matplotlib/tests/test_widgets.py
Outdated
|
|
||
| # Move first vertex before completing the polygon. | ||
| expected_result = [(75, 50), (150, 50), (50, 150)] | ||
| event_sequence = [*polygon_place_vertex(50, 50), |
There was a problem hiding this comment.
unfortunately, we have to support python 2.7 and 3.4, so this won't work :/
| ax = get_ax() | ||
|
|
||
| ax._onselect_cnt = 0 | ||
| def onselect(vertices): |
|
Thanks phobson. Glad you like it. |
…r additional coverage.
| When a polygon is completed or modified after completion, | ||
| the `onselect` function is called and passed a list of the vertices as | ||
| ``(xdata, ydata)`` tuples. | ||
| useblit : bool, optional |
There was a problem hiding this comment.
Interactive is not documented.
lib/matplotlib/widgets.py
Outdated
| The markers for the vertices of the polygon are drawn with | ||
| `markerprops`. The default is ``dict(marker='o', markersize=7, mec='k', | ||
| mfc='k', alpha=0.5)``. | ||
| vertex_select_radius : int |
There was a problem hiding this comment.
and would this be better as a float?
|
This looks useful 👍 attn @blink1073 I have completely lost track of where you are at on other selector work. |
tacaswell
left a comment
There was a problem hiding this comment.
Modulo minor documentation changes.
|
@tacaswell i can add support for non-interactive. i think i will implement it so that the only difference with interactive mode is that the polygon is automatically cleared after the selection is made. alternatively, does anyone think that i should also inhibit the move_vertex and move_all functions that are currently allowed before the polygon has been completed? in other words, the way it currently works is that if you haven't closed the polygon yet, you can move all the vertices or move a single vertex by holding shift or ctrl. should i disable this in non-interactive to be more consistent with the other widgets? |
|
On a second reading, just drop |
|
This is great! I had tried and given up on a similar tool in another PR. |
|
My bad on the |
|
@blink1073, glad you like the tool. i found your PR after thomas tagged you. the PR makes a lot of good points. i did find that i had to put in some special logic to work around logic in |
|
@bduick Ah, sorry my comments were cryptic. Could you add to the docstring that I think that is the only thing standing between this and merging :) |
|
I should've understood. Anyways, fixed and committed! |
| """Select indices from a matplotlib collection using `PolygonSelector`. | ||
|
|
||
| Selected indices are saved in the `ind` attribute. This tool highlights | ||
| selected points by fading them out (i.e., reducing their alpha values). |
There was a problem hiding this comment.
Doesn't it fade out the points that are not selected?
| 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) |
There was a problem hiding this comment.
Isn't this np.repeat(self.fc, self.Npts)?
There was a problem hiding this comment.
Actually, no, I just tried it out and it's not, but it is equal to np.tile(x, (self.Npts, 1)) if I've understood it correctly.
|
|
||
| def onselect(self, verts): | ||
| path = Path(verts) | ||
| self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0] |
There was a problem hiding this comment.
Does path.contains_points(self.xys) work here?
lib/matplotlib/tests/test_widgets.py
Outdated
| where the first element of the tuple is an etype (e.g., 'onmove', | ||
| 'press', etc.), and the second element of the tuple is a dictionary of | ||
| the arguments for the event (e.g., xdata=5, key='shift', etc.). | ||
| expected_result : list or vertices (xdata, ydata) |
lib/matplotlib/tests/test_widgets.py
Outdated
| assert slider.val == slider.valmax | ||
|
|
||
|
|
||
| def check_polygon_selector(event_sequence, expected_result, selections_cnt): |
There was a problem hiding this comment.
Don't see any reason to abbreviate count.
lib/matplotlib/widgets.py
Outdated
| ``(xdata, ydata)`` tuples. | ||
| useblit : bool, optional | ||
| lineprops : dict, optional | ||
| The line for the sides of the polygon is drawn with `lineprops`. The |
There was a problem hiding this comment.
"... drawn with the properties given by lineprops."
(Also, references do not work to arguments.)
There was a problem hiding this comment.
Can you give me some more info re: "references do not work to arguments"? I'm not sure what you mean.
There was a problem hiding this comment.
@QuLogic, is there anything else I need to do to address this comment?
lib/matplotlib/widgets.py
Outdated
| The line for the sides of the polygon is drawn with `lineprops`. The | ||
| default is ``dict(color='k', linestyle='-', linewidth=2, alpha=0.5)``. | ||
| markerprops : dict, optional | ||
| The markers for the vertices of the polygon are drawn with |
There was a problem hiding this comment.
"... drawn with the properties given by ..."
lib/matplotlib/widgets.py
Outdated
| """Button press event handler""" | ||
| # Check for selection of a tool handle. | ||
| if ((self._polygon_completed or 'move_vertex' in self.state) | ||
| and len(self._xs) > 0): |
There was a problem hiding this comment.
Should be indented one more time.
lib/matplotlib/widgets.py
Outdated
| # Add back the pending vertex if leaving the 'move_vertex' or | ||
| # 'move_all' mode (by checking the released key) | ||
| if (not self._polygon_completed | ||
| and (event.key == self.state_modifier_keys.get('move_vertex') |
lib/matplotlib/widgets.py
Outdated
| # if the polygon is completed or the user is locked on to the start | ||
| # vertex. | ||
| if (self._polygon_completed | ||
| or (len(self._xs) > 3 |
|
@QuLogic, thanks for the thorough review. Your 3 comments on the polygon_selector_demo.py also apply to lasso_selector_demo.py. Can I commit the fixes for the lasso demo in this PR, or should I create a new one? |
|
@bduick which ever is easier for you. |
|
failures look to be due to changing DNS for matplotlib.org and it taking time for those changes to propogate |
|
very cool! |
|
attn @phobson |
Hello matplotlib community! Please checkout the PolygonSelector widget. The demo is pretty straight forward, and basically a copy of the LassoSelector demo. I'm open to any feedback, or suggestions for improvement. Thanks for your consideration.