Skip to content

Clean up RectangleSelector move code #21921

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 2 commits into from
Dec 12, 2021
Merged

Conversation

dstansby
Copy link
Member

PR Summary

I am working towards improving RectangleSelector to allow rotation past +/- 45 degrees. This PR cleans up some of the movement code to:

  • Add a short docstring
  • Add the xy and xy_press variables to make the code more readable
  • Add a block of three lines at the top to determine whether to rotate, resize, or move
  • Use these variables in the if/else blocks below, to make it clear what is going on in each block

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 requested a review from ericpre December 10, 2021 19:29
@dstansby dstansby added this to the v3.6.0 milestone Dec 10, 2021
resize = self._active_handle and not move

if resize:
# If resizing, use xy/xy_press in a de-rotated frame
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
# If resizing, use xy/xy_press in a de-rotated frame
# use xy/xy_press in a de-rotated frame

Optional: The code above aleady says it.

- Re-size
- Contine the creation of a new shape
"""
xy = np.array([event.xdata, event.ydata])
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
xy = np.array([event.xdata, event.ydata])
xy = np.array([event.xdata, event.ydata])

Side note: I'm wondering if a small internal class _Point would make sense for such cases, basically namedtuple-like with arithmetics. So that we can do.

p = _Point(x, y)
2*p
p + (1, 2)
p.x

Arithmetics should still be faster than the numpy overhead.

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.

I am +-0 on this change, because even if it does make slightly more compact, I am not convinced that it makes the code more readable. To me this is a matter of taste and it doesn't really matter: I don't see it as an improvement or a regression!
Typically, for a matplotlib developer event.xdata or event.x has a meaning, while in case of xy or xy_press, this is a redirection and one need to look up what it is.

An alternative would be to add some convenience properties (xy, xydata) to the Event class. These properties could also support basic arithmetic, as suggested by @timhoffm in its comments.

@dstansby
Copy link
Member Author

A good point - I've dropped the commit that changes the variable names - hopefully what remains is definitely an improvement?

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.

Yes, sorry, I have missed these other improvements!

@timhoffm timhoffm merged commit 6e15597 into matplotlib:main Dec 12, 2021
@dstansby dstansby deleted the rect-sel-clean branch December 13, 2021 11:21
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.

3 participants