Skip to content

ENH: Add centered-gouraud shading to pcolormesh#31362

Open
lucaznch wants to merge 1 commit intomatplotlib:mainfrom
lucaznch:centered-gouraud
Open

ENH: Add centered-gouraud shading to pcolormesh#31362
lucaznch wants to merge 1 commit intomatplotlib:mainfrom
lucaznch:centered-gouraud

Conversation

@lucaznch
Copy link

PR summary

This PR adds the centered-gouraud shading to Axes.pcolormesh. It is a variant of gouraud that adds support for grids that are one larger than the data in each dimension. In this situation, centered-gouraud automatically converts it to match the data by taking the centers of each quadrilateral, averaging its four corners, and then applies Gouraud shading.

  • Why is this change necessary / what problem does it solve?

    pcolormesh with shading='gouraud' requires the grid to match the shape of the data. If the user is working with a grid one larger in each dimension and tries to use gouraud, a TypeError is raised. centered-gouraud solves this by allowing such grids without requiring users to reshape them.

  • What is the reasoning for this implementation?

    Rather than modifying the existing gouraud shading, a new shading was added following a discussion with @timhoffm on the issue this PR addresses.

Example:

import matplotlib.pyplot as plt
import numpy as np

nrows = 3
ncols = 5
Z = np.arange(nrows * ncols).reshape(nrows, ncols)
x = np.arange(ncols + 1)
y = np.arange(nrows + 1)

fig, ax = plt.subplots()

ax.pcolormesh(x, y, Z, shading='flat')
ax.pcolormesh(x, y, Z, shading='gouraud') # doesn't work
ax.pcolormesh(x, y, Z, shading='centered-gouraud') # works

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

@github-actions github-actions bot added topic: rcparams Documentation: examples files in galleries/examples labels Mar 24, 2026
@github-actions
Copy link

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.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

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
@lucaznch
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

plt.pcolormesh shape mismatch when shading='gouraud'

1 participant