Skip to content

Preserve Matplotlib grid settings in pcolormesh - #11923

Merged
Illviljan merged 2 commits into
pydata:mainfrom
Arham-Begani:fix-11919-pcolormesh-grid
Nov 21, 2026
Merged

Illviljan merged 2 commits into
pydata:mainfrom
Arham-Begani:fix-11919-pcolormesh-grid

Conversation

@Arham-Begani

Copy link
Copy Markdown
Contributor

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

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]>
@welcome

welcome Bot commented Nov 20, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@headtr1ck

Copy link
Copy Markdown
Collaborator

Please update the PR description to follow the template.

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx!

@headtr1ck headtr1ck added the plan to merge Final call for comments label Nov 21, 2026
@Illviljan
Illviljan merged commit b369e48 into pydata:main Nov 21, 2026
64 checks passed
@welcome

welcome Bot commented Nov 21, 2026

Copy link
Copy Markdown

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

@headtr1ck headtr1ck mentioned this pull request Nov 22, 2026
6 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments topic-plotting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants