Skip to content

ENH: Support mixed shading in pcolor/pcolormesh#31918

Open
AbiramiR-27 wants to merge 1 commit into
matplotlib:mainfrom
AbiramiR-27:mixed-shading-pcolor
Open

ENH: Support mixed shading in pcolor/pcolormesh#31918
AbiramiR-27 wants to merge 1 commit into
matplotlib:mainfrom
AbiramiR-27:mixed-shading-pcolor

Conversation

@AbiramiR-27

Copy link
Copy Markdown

Description

Allow specifying shading style individually for x and y axes as a 2-tuple.
Resolve 'auto' individually per axis.
Added validation for 2-tuple configurations in rcsetup.
Added comprehensive tests covering correct resolution, shape checks, and error cases.

PR summary

Currently, pcolor and pcolormesh support coordinate grids that are either the same size as the 2D data array (shading='nearest'), or one larger than the array (shading='flat'). However, it was not possible to plot a dataset where the coordinate grid is defined by cell centers along one dimension and cell edges along the other.

This PR introduces support for mixed shading styles by allowing a 2-tuple of shadings (e.g. shading=('nearest', 'flat') or shading=('flat', 'nearest')). It also updates shading='auto' to automatically resolve to a mixed shading configuration depending on the coordinate and data dimensions.

Implementation Details:

Validation: Updated validation in rcsetup.py via validate_pcolor_shading to support a 2-tuple of values, ensuring that 'gouraud' cannot be mixed with other shading styles (which raises a ValueError).
Grid Generation: In _axes.py: _pcolorargs, the shading parameter is unpacked/normalized. If either dimension has 'nearest' shading, its coordinate centers are interpolated to cell edges, while the other dimension is left as-is if it is already 'flat'. This results in a final rectangular grid of cell corners which is then rendered with 'flat' shading.
Compatibility: Standard single-string shadings ('flat', 'nearest', 'gouraud', 'auto') continue to work exactly as before.

Example usage:

python
import matplotlib.pyplot as plt
import numpy as np

Data shape: 4 rows, 5 columns

Z = np.random.rand(4, 5)

X matches columns (5 centers -> nearest)

X = np.arange(5)

Y matches cell edges (5 edges for 4 rows -> flat)

Y = np.arange(5)
fig, ax = plt.subplots()

Automatically resolves to shading=('nearest', 'flat')

pcm = ax.pcolormesh(X, Y, Z, shading='auto')
plt.show()

closes #31607

AI Disclosure

Antigravity was used to assist in writing parts of the implementation logic and formatting unit tests.

PR checklist

  • [✓]"closes [ENH]: Mixed shading in pcolor/pcolormesh #31607" is in the body of the PR description to link the related issue
  • [✓]new and changed code is tested
  • []Plotting related features are demonstrated in an example
  • []New Features and API Changes are noted with a directive and release note
  • [✓]Documentation complies with general and docstring guidelines

@github-actions

Copy link
Copy Markdown

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 discourse chat 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.

)

- Allow specifying shading style individually for x and y axes as a 2-tuple.
- Resolve 'auto' individually per axis.
- Added validation for 2-tuple configurations in rcsetup.
- Added comprehensive tests covering correct resolution, shape checks, and error cases.
@AbiramiR-27 AbiramiR-27 force-pushed the mixed-shading-pcolor branch from 50eae5b to 273ba89 Compare June 18, 2026 09:25
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.

[ENH]: Mixed shading in pcolor/pcolormesh

1 participant