Skip to content

MNT: Remove auto-flattening of input data to pcolormesh #24638

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 1 commit into from
Jan 30, 2023

Conversation

greglucas
Copy link
Contributor

PR Summary

Opening this as a draft for discussion.

If someone passes in a 2-D array to .pcolormesh() should we automatically call .ravel() on it? See question from @jklymak here: #24619 (comment) Flattening is what historically has been done, but we've been moving towards QuadMesh being a primary 2-D collection lately and could get rid of this restriction on flattening input.

The updates here are all primarily surrounding what shapes the returned arrays are from the function calls and how to compare them, so drawing hasn't changed. Also note that this would introduce a deviation between pcolor(z2d).get_facecolors() == colors1d which returns the flattened array, while pcolormesh(z2d).get_facecolors() == colors2d returns the original shape of the array passed in.

Collection classes have a lot of "1D list of objects" method handling in them, but in QuadMesh there is additional handling for the known 2-D structure of the arrays (mesh in the name). pcolor() returns a PolyCollection which is a 1D list of Polygons, and if there are masked elements it will actually shrink the list and not add that Polygon, so it would require more book-keeping (maybe adding a new PolyMeshCollection just for pcolor) to get the 2-D logic right.

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

Comment on lines 1014 to +1025
# Turn it back on by restoring the array (must be 1D!):
pc.set_array(z.ravel())
pc.set_array(z.ravel() if pcfunc == plt.pcolor else z)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both of these situations feel undesirable...

  • The must be 1D! is the way it currently is and doesn't feel right after we've initialized our array with the 2D version pcolor(z).
  • It also doesn't feel great to change this and have to do the if pcfunc ... and change between 1D and 2D depending on which pcolor incantation I chose.

Copy link
Member

Choose a reason for hiding this comment

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

The flattening in pcolormesh is a historical artifact; I think the move to accept it as fundamentally 2-D is good.
Pcolor is rarely a better choice than pcolormesh. When it is, it is likely because it can generate a set of
colored quadrilaterals that don't have to fill out a 2-D domain. I think we should embrace this difference between
pcolormesh and pcolor, so I don't have a problem with your special-casing in the test. I'm open to discussing
ideas for backwards-compatible improvements to pcolor, so that it could also take a 2-D array in set_array,
but it doesn't seem like a priority.

@greglucas greglucas added this to the v3.8.0 milestone Dec 15, 2022
@greglucas greglucas marked this pull request as ready for review January 19, 2023 02:35
@greglucas greglucas mentioned this pull request Jan 19, 2023
1 task
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 the required rebase, I think this is fine.

@efiring
Copy link
Member

efiring commented Jan 26, 2023

This is included in #25027, correct?

If someone passes in a 2-D array, we should keep that in the array data
@greglucas
Copy link
Contributor Author

This is included in #25027, correct?

Yes, I've rebased that PR onto this branch to get these updates.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

Yay!

@mwaskom
Copy link

mwaskom commented Feb 6, 2023

Ping; this PR apparently breaks seaborn (see issue linked above) and presumably other libraries that make assumptions about the shape of the data attached to artists added by axes functions.

I appreciate that there isn't a straightforward path to noisily warning about upcoming changes in artist properties, but could I ask that this change be delayed for at least a version cycle or two to allow downstream packages to adjust?

@greglucas
Copy link
Contributor Author

Thanks for the heads up. Note that this isn't targeted at 3.7, so we do have some time to think about the best approach here.

I guess we could warn whenever get_facecolors() or get_array() would return a flattened array. But there wouldn't be a way to easily suppress the warning and get the new behavior without a temporary keyword argument which would then also require removal downstream later.

I agree, we should probably do some kind of warning/compatibility helper here.

@jklymak
Copy link
Member

jklymak commented Feb 6, 2023

Let's discuss on an issue?

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