8000 Cleanup hexbin. by anntzer · Pull Request #13690 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Mar 17, 2019

Use & instead of * for boolean arrays; vectorize accumulation
instead of iterating over individual entries by hand; misc. style
cleanups.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Copy link
Member
@efiring efiring left a comment

Choose a reason for hiding this comment

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

Apart from my dislike of the last change, this is a nice little simplification. I didn't even know about the add.at method.

polygon = np.zeros((6, 2), float)
polygon[:, 0] = sx * np.array([0.5, 0.5, 0.0, -0.5, -0.5, 0.0])
polygon[:, 1] = sy * np.array([-0.5, 0.5, 1.0, 0.5, -0.5, -1.0]) / 3.0
polygon = np.column_stack([
Copy link
Member

Choose a reason for hiding this comment

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

Minor matter of taste: I think that in a case like this, the original form is actually more readable because it immediately shows the shape of polygon. It's also more efficient, although in this case that doesn't matter.
(To top it off, I consider this unindented location of closing parenthesis to be un-pythonic.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My preference is for arrays to be built in a single expression:

  • you don't need to track until where does the array initialization occur -- it's done when the expression is over.
  • you don't need to wonder whether the zeros set by np.zeros() actually matter (are some of them not being overwritten later? ou could this really have been np.empty()? -- not so much for the performance gain for not zeroing the array, but the semantic info that "everything gets initialized below).

FWIW some timings suggest that the fastest way to construct this array are

polygon = np.array(
    [[0.5, 0.5, 0.0, -0.5, -0.5, 0.0], [-0.5, 0.5, 1.0, 0.5, -0.5, -1.0]])
polygon[0] *= sx
polygon[1] *= sy / 3
polygon = polygon.T

and

polygon = [sx, sy/3] * np.array([[.5, -.5], [.5, .5], [0, 1], [-.5, .5], [-.5, -.5], [0, -1]])

a close second (actually I kind of like this form)
... not that it matters here.

As for the unindented closing parenthesis, it is explicitly allowed by PEP8 as the last item of https://www.python.org/dev/peps/pep-0008/#indentation.

Anyways, none of this really matters, if you insist on reverting this part of the change let me know and I'll do it, that's totally fine with me.

Copy link
Member

Choose a reason for hiding this comment

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

Allowed doesn't necessarily mean good; but it's a minor matter.
Regarding your "close second": I hadn't known that multiplying a list times an array would work--I thought one would have to explicitly make the list an array.
I actually like that form, too. It's concise, and most clearly shows the sequence of coordinates.
But it's up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hadn't known that multiplying a list times an array would work

As long as one of the operands is an array, things work.

Pushed the "close second" version.

Use `&` instead of `*` for boolean arrays; vectorize accumulation
instead of iterating over individual entries by hand; misc. style
cleanups.
@efiring efiring merged commit 4e51619 into matplotlib:master Mar 18, 2019
@anntzer anntzer deleted the hexbin branch March 18, 2019 17:10
@QuLogic QuLogic added this to the v3.2.0 milestone Jul 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0