-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix inconsistent behavior in pcolormesh with Gouraud shading #9594
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
I don't understand; what is wrong with just setting 'allmatch' to True if Gouraud shading is used, and letting it error out when the dimensions don't match? Then it is up to the user to decide how to handle the situation. A little more documentation, and maybe more info in the error message, seems to me to be all that is needed. Otherwise we are trying to ignore the fundamental difference between Gouraud shading and normal pcolormesh. But if this suggestion is rejected, then my fallback would be to chop the offending extra values off the edges of the X and Y input arrays. This would be consistent with the present Matlab-compatibility quick-and-dirty practice of chopping edges off the Z array with flat shading. |
I think we should instead just document properly what are the required dimensions (with flat shading, X and Y are boundaries; with gouraud shading, they match "centers") and error out cleanly in other cases (with a deprecation for the case of flat shading):
Note that right now, with mismatched dimensions, you can get, e.g.
but the docstring of pcolormesh says nothing about X and Y's dimensions. |
I basically followed the suggestions in the corresponding issue. I totally agree that if the user provides inconsistent data an error should be thrown. But I guess the argumentation is that the for flat shading the case when inconsistent data is provided IS handled while for |
Yes please don’t drop support for same-dimensions for pcolormesh! I think the case for adding support for n+1 gouraud is strong as well. Often when pcoloring you just want to switch between methods, and having to change x and y is a pain. |
@jklymak I have the feeling that what you are asking for is really proper interpolation support for |
I don't think so. I'm suggesting that if I input |
TBH it's exactly the kind of behavior I want to stamp out. More precisely, the behavior proposed in this PR (ie what you propose) for On the other hand, I really believe we should never silently drop (even a small part of) user input, as is currently the case for |
I'd say 85% of the plotting I do is with matrices where z is MxN, x is N and y is M. Semantically, in my mind, x = [1 3 6] and z has three columns mean I want the center of the facet defined by z. I appreciate the other semantic is valid - that x = [0 2 4 8] means the edge of the facets and some sort of interpolation happens between them - but I don't see one set as superior to the other. I don't think Matplotlib should make users be aware of implimentation details unless they need to be made aware of them. Of course if someone zooms in on the individual pixels and gets upset about the alignment, then they need to figure out the implimentation details and they should be clear in the docs (which I think they are). But 98% of users just want the plot to work w/o a bunch of futzing... |
I'm more with @anntzer on this: move in the direction of clarity, explicitness, and correctness, with helper functions as needed for ease of use. Keyword arguments could also be used to explicitly request certain behaviors. I think that with careful design we can have both correctness and ease of use. And less confusion. |
Ok but just to be clear about this 99.9% of netcdf files (for instance) have variables that have the same dimensions as the accompanying coordinate variables. It’s the usual way to layout data. I don’t think the default for pcolormesh should be that the user has to mess with a perfectly normal data layout and figure out where the coordinate bin edges are located. |
OK, so I tried to combine the points brought up in the discussion and came up with the following changes:
|
This will need an entry in api_changes as it will break various size data working and a Would a reasonable middle ground to have the kwarg default to |
This discussion reminds me of #5855 where I was on the other end of the argument (and that was a case where AFAICT there was really only one way to fix the data, and that involved no data point being ignored...). |
In my opinion:
Further, I think the solution for |
… color grid also for gouraud shading.
… of X and Y for Gouraud shading in case of dimension mismatch.
Just to clarify: The new To enable the interpolation the user has to actively set |
I'm just arguing that you shouldn't have the flag, and just do the interpolation no matter what. If you do go the way of having the flag, then the error message should at least mention it. |
…rd in error message.
@jklymak, I decided to have the flag because from the previous discussion it seems that the majority is against having the interpolation by default. But as you suggested I added the mention of the interpolate_grids keyword argument. @tacaswell Is the api_changes entry still required even if this will not break the API? |
This will need revision based on new pcolormesh behaviour. Not sure if @simonpf is still interested in pursuing this. |
In the intervening time we have made some changes to this signature, it is now https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pcolormesh.html#matplotlib.axes.Axes.pcolormesh and addded a shading Given that these changes over took this PR I am going to close it. I apologize that this got stalled in review @simonpf and thank you for you work! I hope we will hear from you again. |
PR Summary
As described in issue 8422 behavior of
pcolormesh
was inconsistent when using Gouraud shading. The documentation states that the X and Y grids, if given, should have the same or one row and column more than the color grid C. However, if Gouraud shading is used, this would result in an error.To handle the case where the X and Y grid have on row and column more than the color grid, I propose to interpolate the grids linearly to obtain the center points of each quadrilateral. This should ensure that the resulting image has the right colors at the centers of each of the quadrilaterals given.
The disadvantage, however, is that the plotted grid will cover a smaller range than X and Y grids given to
pcolormesh
. It is up for discussion if the grid should be so avoided by padding the resulting grids.Example
PR Checklist