ENH: Support mixed shading in pcolor/pcolormesh#31918
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 discourse chat 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. |
49af62b to
50eae5b
Compare
50eae5b to
273ba89
Compare
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