-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Adding a get_coordinates() method to Quadmesh collections #18472
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
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.
Seems simple enough
This is probably okay, but I'm unsure about the name. For |
See the linked issue for other ideas too. My initial idea was to call it |
Yeah, I didn't think about the name. |
I really didn't want to go down the rabbit hole of adding matplotlib/lib/matplotlib/collections.py Lines 1994 to 2001 in 6cf9219
which are promptly reshaped to (ny, nx, 2) to store internally, so which one of those versions do we support?
Digression...Honestly, my real preference would be to deprecate the flattened version of Quadmesh altogether and only take the actual 3D mesh as input (or you could do xcoord, ycoord as well to make it more like pcolormesh). There really isn't a need to pass meshWidth and meshHeight into the constructor, you could easily just get that from the shape of the coordinates that were passed in. Now that I'm writing this out, I'm wondering if there would be buy-in from others on the idea of deprecating the flattened QuadMesh? I don't think too many people are creating QuadMesh's from the constructor, but rather it is usually a returned Class from pcolormesh and other higher-level plotting functions. So, a deprecation may not hit too many people actually and would hopefully benefit them by not forcing them to reshape/flatten. |
I agree that flattened QuadMesh coordinates are awkward. It's the relation of 4 points that makes up a quadrilateral. Within the flattened data, we still imply a certain ordering in rows/columns. If we/the user gets that wrong, we'll end up with a total mess. Example (changing the order of points by reshape (4, 3) -> (3, 4)): Show code
So, it seems better to make the relation explicit by a 2D shape. There are multiple parts to this:
It will in particular be hard if not impossible to change everything in the fourth category. The 1D logic of ScalarMappable and Collection will always shine through in some places. However, that probably should not hold us up from changing 1., 2., and maybe 3. The alternative is having a consistently unusable API everywhere. |
b5c5b9b
to
fd9521e
Compare
lib/matplotlib/collections.py
Outdated
Return the x and y vertices of the mesh. | ||
|
||
The shape of the returned coordinates is | ||
(*meshWidth* + 1, *meshHeight* + 1, 2). |
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.
Does the user know what meshWidth/Height are?
Maybe a bit more explanation?
(*meshWidth* + 1, *meshHeight* + 1, 2). | |
(*meshWidth* + 1, *meshHeight* + 1, 2), where *meshWidth* and *meshHeight* are the size of the | |
mesh, passed in when the Artist was initialized. |
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.
Hmm... Good point. Unfortunately, the initialized wording isn't entirely clear to me either because pcolormesh
can auto-expand the coordinates for you (which is why I wanted this public in the first place). The docstring for QuadMesh gives some info on what meshWidth/meshHeight are, so maybe that is sufficient for now?
https://matplotlib.org/stable/api/collections_api.html#matplotlib.collections.QuadMesh
lib/matplotlib/collections.py
Outdated
Return the x and y vertices of the mesh. | ||
|
||
The shape of the returned coordinates is | ||
(*meshWidth* + 1, *meshHeight* + 1, 2). |
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.
We're removing the names meshWidth/meshHeight from code and docs.
Some thing like this should do:
Return the x and y vertices of the mesh. | |
The shape of the returned coordinates is | |
(*meshWidth* + 1, *meshHeight* + 1, 2). | |
Return the vertex positions of the mesh as (M, N, 2) array. | |
M, N are the number of rows / columns in the mesh. | |
The last dimension specifies the components (x, y). |
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.
Sounds good. The only issue is that it may be confusing to use (M, N) here to refer to the vertices when other places use that notation to refer to the number of Polygons/Quads.
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.
Yeah, technically this might be (M+1, N+1, 2)
array. Would that be better?
More generally, I'm switch back and forward between the +/-1 interpretations, i.e. what is the canonical quantity for the number (M, N), the quadrilaterals or the vertices/coordinates?
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.
Most of the previous documentation has had (M, N) as the number of quadrilaterals in the mesh, and to me that makes intuitive sense since that is the quantity we are most interested in. I'd vote for changing this to (M+1, N+1, 2) and explaining that here, but I also don't have a real strong preference.
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 do - the data is MxN, the co-ordinates follow that. You could argue the coordinates are more important if they are twisting out some complicated shape, but 99.9% of the time this is tracking out a regular grid.
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 changed the text to explicitly call out and distinguish the quads and vertices in the statement.
Return the vertices of the mesh as an (M+1, N+1, 2) array.
M, N are the number of quadrilaterals in the rows / columns of the mesh, corresponding to (M+1, N+1) vertices. The last dimension specifies the components (x, y).
86c6e75
to
f52bbc1
Compare
This returns the coordinates used to create the Quadmesh collection.
f52bbc1
to
453abb1
Compare
Thanks @greglucas for keeping working on this and going through all the review cycles with us! |
PR Summary
This returns the coordinates used to create the Quadmesh collection. These can be useful for knowing what vertices were used to generate the mesh returned from
pcolormesh
.closes #18399
PR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
andpydocstyle<4
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).