-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Cleanup hexbin. #13690
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
Cleanup hexbin. #13690
Conversation
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.
Apart from my dislike of the last change, this is a nice little simplification. I didn't even know about the add.at
method.
lib/matplotlib/axes/_axes.py
Outdated
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([ |
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.
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.)
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.
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.
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.
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.
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.
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.
Use
&
instead of*
for boolean arrays; vectorize accumulationinstead of iterating over individual entries by hand; misc. style
cleanups.
PR Summary
PR Checklist