ENH: Extend gouraud shading to support data at quadrilateral centers#31362
ENH: Extend gouraud shading to support data at quadrilateral centers#31362lucaznch wants to merge 7 commits intomatplotlib:mainfrom
Conversation
|
Thank you for opening your first PR into Matplotlib! If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process. You can also join us on gitter for real-time discussion. For details on testing, writing docs, and our review process, please see the developer guide. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
7b76875 to
15d8932
Compare
|
I realized I forgot to run boilerplate.py to update pyplot.py, which is why some tests were failing. Ive just amended the commit and pushed the updated branch! |
scottshambaugh
left a comment
There was a problem hiding this comment.
I kind of prefer extending 'gouraud' rather than making a separate 'centered-gouraud', but can see @timhoffm rationale for keeping them separate.
Code and docs all look good to me!
|
Thank you for the review and suggestion. I updated that part to use f-strings. |
There was a problem hiding this comment.
My mental model is that we need to primarily distinguish the data semantics: Are the values at the corners of the quadrilaterals (n_grid = n_data + 1) or at the centers (n_grid = ndata)?
Therefore switching should only work between "nearest" <-> "gouraud" and "flat" <-> "centered-gouraud"
| discrete | continuous | |
|---|---|---|
| data at corners | "nearest" | "gouraud" |
| data at centers | "flat" | "centered-gouraud" |
"auto" just selects between "flat" and "nearest" based on the geometry.
We probably should rewrite the whole shading logic more in these terms. While I don't require this to be part of this PR, the descriptions added here should be consistent with that logic.
Edit: I had mixed up "nearest" and "flat" above. Now corrected.
galleries/examples/images_contours_and_fields/pcolormesh_grids.py
Outdated
Show resolved
Hide resolved
|
If you want to be completely parallel you could have "flat-gouraud" and "nearest-gouraud" and then have "gouraud" be like "auto" and just do the right thing. |
Hm, while this would be structurally the same, the naming would be a bit awkward. "flat" stems from the idea that there is a constant value across the quadrilateral. "nearest" meas "take the color from the nearest grid node. Both do not rhyme with a continous shading that gouraud implies. I believe if we want more consistent naming we would need to rethink the the whole set of names "constant" and "gouraud" would be the automatic ones (and discourage "auto" in favor of "constant"). Not sure about the grid-specific ones; maybe "nodes-constant", "centered-condstant", "nodes-gouraud", "centered-gouraud". It's a bit the question whether we want to advertize/encourage automatic response to the shape constallations, or whether we prefer explicit specification that the data points are meant to be on the grid nodes or the quadrilateral centers. |
|
Fair enough, that is awkward. Just to review the context of the original changes: the old behaviour of Here the problem is less, and the opposite. Occasional users who have x and y having one ore element than Z in each dimension cannot make a plot. I think we should just help those users by automatically plotting the data at the centres, and maybe emiting a warning. But I'm fine if we want to have a different named option to allow this. |
Implement a variant of Gouraud shading that supports grids with dimensions one greater than the data in each dimension. Add tests and documentation. Update type hints and rcParams validation. Include a What's New entry. Fixes matplotlib#8422
b036809 to
643a546
Compare
|
Thanks for the feedback. Ive made the suggested changes and pushed everything. I noticed however that several automated tests are failing. I’m not sure if these are related to my changes or unrelated. |
|
We still need to make a decision. On whether automatic behavior based on the grid vs data shape is desired and should be the default (in which case gouraud should cover both). I've originally been in favor of dedicated names for all the use cases to have the analogy with 'nearest' and 'flat' and force an explicit decision on how you want to render your data. But can see the appeal of an automatic setting so that "it just works". |
|
I suspect the majority of users do not bother passing “flat” or “nearest” to the shading parameter but just leave it to “auto”. Could we deprecate those options for shading and introduce a new parameter that distinguishes whether x,y represent “centers” or “corners” for those who really want to nail it down? So we end up with something like
|
|
From the discussion so far, it seems there are two main approaches being considered: 1. Extend
2. Introduce
Another approach suggested by @rcomer: 3. Separate rendering style from data semantics
My current implementation follows approach 2. I’m happy to adapt the PR depending on the preferred direction. |
|
I like @rcomer's suggestion of using two keyword parameters to decouple the shading type from the location type. That said, I'm not a fan of calling the option |
|
If Then, I'm rather tempted to aim at In theory, we could alternatively promote 'nearest' or 'flat' to the general one if you feel that naming would be much better. It's only slightly weirder than promoting 'grouraud'. |
|
I agree 'flat' and 'nearest' are bad terms for Gouraud. Given that, I'd vote for 1) above - just fix the data for the user, and emit a warning if it has been fixed. I think this case will be very rare, and a user who wants to suppress the warning can supply the properly shaped data. That way the toggle is |
|
About your
Regarding this PR, I’m thinking of proceeding with approach 1, extending The shading simplification idea seems like a great improvement. I’d be happy to either include it in this PR if that is preferred, or work on it in another PR. |
|
Yes, 'constant' and 'gouraud' would handle both cases and automatically decide what to do.
Yes, but without the warning. If we have an automatism, that's intended behavior, so a warning is not justified (in the same way that "auto" does not warn). Let's focus this PR on the implementation. We can have a follow-up to discuss whether and how to evolve the API. |
|
Understood, thanks for the clarification. In the meantime I noticed two minor things in the docstrings that could maybe be improved. |
|
I'd still suggest a warning as we are dropping data (the coords of the cell edges in favour of the midpoints). "Auto" doesn't drop anything. |
|
I suppose the warning depends on how this automatic handling is intended to be presented, whether it is a fallback for incompatible grids one larger than C, or an extension of gouraud to support that same case (and therefore needing to update user documentation (specifically this example). Given that it was mentioned that this case is not very common, i think it makes sense to go with a warning. |
|
Updated the implementation to handle grids one larger than C in |
|
I would have a problem with if the only way to use centered Gouraud shading always produces a warning. A regular feature must always be usable without a warning. In addition, this mustn’t be a „fallback for incompatible grids“. An automatism is only justified if every outcome is expected for the respective inputs, i.e. we must be able to assume that the user knows whether their data is to be on the grid nodes or centers. If we cannot, doing something with a warning is the wrong approach. Instead we then cannot do the automatism, but would have to resort to two targeted shading variants that the user would have to specify explicitly. |
|
Understood, thanks. |
- Extend gouraud shading to support data defined at the centers of quadrilaterals. - Add tests. - Add and update documentation. - Add a What's New entry.
45849df to
081efc4
Compare
|
Updated the implementation following the discussion: extended Added and updated docs (including the two fixes in the pcolormesh API docs I pointed out above), added tests, and included a What's New entry. |
|
|
||
| # Data at centers, requires X and Y the same shape as Z. | ||
| ax.pcolormesh(x[:-1], y[:-1], Z, shading='nearest') | ||
| ax.pcolormesh(x[:-1], y[:-1], Z, shading='gouraud') |
There was a problem hiding this comment.
| ax.pcolormesh(x[:-1], y[:-1], Z, shading='gouraud') | |
| ax.pcolormesh(x[:-1], y[:-1], Z, shading='centered-gouraud') |
Also this should probably use a ..plot: directive so that the release note actually shows the plot with the differences and runs this code


PR summary
This PR extends the
gouraudshading inAxes.pcolormeshto support data defined at the centers of quadrilaterals, where X and Y are one larger than Z in each dimension. In this situation,gouraudautomatically converts the grid to match the data's shape by replacing each quadrilateral with a point at its center, computed as the average of its four corners, and then applies Gouraud shading.Why is this change necessary / what problem does it solve?
pcolormeshwithshading='gouraud'requires the data to be defined at the corners of quadrilaterals, meaning X, Y and Z must have the same shape. If the user is working with data defined at the centers, so that X and Y are one larger in each dimension, and tries to useshading='gouraud', a TypeError is raised. Extendinggouraudsolves this by allowing such grids without requiring users to manually reshape them.What is the reasoning for this implementation?
Following discussion on the issue and in this PR, the approach taken is to extend the existing gouraud shading. This keeps the API simpler while allowing gouraud to handle both grid/data configurations.
Example:
Closes #8422
AI Disclosure
I used AI tools to help proofread and improve the grammar of this PR description. The code logic and implementation are my own.
PR checklist