Repository navigation
Preserve Matplotlib grid settings in pcolormesh - #11923
Conversation
Matplotlib 3.7 removed the automatic grid removal that prompted the workaround. Let supported versions respect the grid visibility already set on the axes. Co-authored-by: Codex <[email protected]>
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
|
Please update the PR description to follow the template. |
headtr1ck
left a comment
There was a problem hiding this comment.
Thanks @Arham-Begani, removing the line is the right fix.
Some history: ax.grid(False) was added in #7219 to silence a warning in our plotting tests. Matplotlib 3.5 deprecated the automatic removal of grids by pcolor and pcolormesh and warned about it. The PR already asked whether we should rather follow matplotlib's default. We now require matplotlib >= 3.10, where pcolormesh leaves the grid alone and doesn't warn, so the line has no purpose anymore and only overrides the user's style (#11919).
I don't think we need the new test, though: it tests matplotlib's handling of axes.grid rather than anything xarray does. And if matplotlib warned again, our filterwarnings setting already turns warnings raised from xarray code into errors in the existing pcolormesh tests. Could you remove it?
Co-authored-by: Codex <[email protected]>

When a Matplotlib style enables grid lines, DataArray.plot.pcolormesh() turns them off by calling ax.grid(False) just before drawing. Removing that call lets the plot respect the grid setting on its axes. The regression test checks both directions: an enabled grid stays visible and a disabled grid stays hidden. Both cases passed on Matplotlib 3.10.9 and 3.11.2