Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add option of bounding box for PolygonSelector #21830

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 6 commits into from
Jan 5, 2022

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented Dec 1, 2021

PR Summary

This allows one to put a bounding box around a PolygonSelector, that can be used for resizing and moving (and eventually rotating) the selector points.

import matplotlib.pyplot as plt
from matplotlib.widgets import PolygonSelector

fig, ax = plt.subplots()
sel = PolygonSelector(ax, lambda *args, **kwargs: None, draw_box=True)
plt.show()
polygon.mov

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@dstansby dstansby force-pushed the polygon-box branch 2 times, most recently from 5847eda to 4ac8e37 Compare December 3, 2021 11:06
@dstansby dstansby added this to the v3.6.0 milestone Dec 3, 2021
@dstansby
Copy link
Member Author

dstansby commented Dec 3, 2021

This should be ready for review now

Copy link
Member

@ericpre ericpre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works nicely, I made a couple of comments/suggestions.

Copy link

@dhomeier dhomeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and updates; I have tested this with a method in astropy/regions#406 that uses this new functionality and see that some early quirks have already been resolved!
A general issue with that regions method, which connects a custom PolygonSelector, is that creating a complete instance is somewhat tedious – adding the vertices and then triggering a redraw and finally marking the selector _selection_completed; for most of this I have not found public methods for the required operations. Having those, analogously to extents etc. in RectangleSelector would be a plus (but not necessary from this PR).

@dstansby dstansby marked this pull request as ready for review December 9, 2021 21:43
Copy link
Member

@ericpre ericpre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, it just need the test to be fixed!

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it should be called draw_bbox, or even draw_bounding_box (since we seem to have pretty verbose names in widgets already.)

Comment on lines +1484 to +1409
event_sequence = (polygon_place_vertex(*verts[0]) +
polygon_place_vertex(*verts[1]) +
polygon_place_vertex(*verts[2]) +
polygon_place_vertex(*verts[3]) +
polygon_place_vertex(*verts[0]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
event_sequence = (polygon_place_vertex(*verts[0]) +
polygon_place_vertex(*verts[1]) +
polygon_place_vertex(*verts[2]) +
polygon_place_vertex(*verts[3]) +
polygon_place_vertex(*verts[0]))
event_sequence = (
*polygon_place_vertex(*verts[0]),
*polygon_place_vertex(*verts[1]),
*polygon_place_vertex(*verts[2]),
*polygon_place_vertex(*verts[3]),
*polygon_place_vertex(*verts[0]),
)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't do this since the rest of test_widgets.py uses the + notation. I'd be happy to put in a PR to change it all in one go though?

Update polygon every time bounding box is moved

Allow box customisation

Remove del

Prevent rotation on polygon bounding box

Fix line length

Cleanup transform logic

Don't use dicts as keyword defaults

Minor doc fixes
Run exising polygon selector tests with draw_box=True
Trigger events on canvas in test

Test doc fixes
@dstansby
Copy link
Member Author

Thanks for reviewing. I changed the argument name to draw_bounding_box, but left the internal variables as _draw_box for simplicity. All other comments should be addressed. I also cleaned up the commit history a bit - sorry if this makes it a bit trickier to re-review...

Co-authored-by: Elliott Sales de Andrade <[email protected]>
Co-authored-by: Elliott Sales de Andrade <[email protected]>
@QuLogic QuLogic merged commit 37c9b04 into matplotlib:main Jan 5, 2022
@dstansby dstansby deleted the polygon-box branch January 5, 2022 22:34
@dstansby dstansby mentioned this pull request Jan 8, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants