Skip to content

FIX: Support single row/column grids for nearest shading - #32405

Open
lucaznch wants to merge 1 commit into
matplotlib:mainfrom
lucaznch:fix-nearest-single-row-column-grid
Open

lucaznch wants to merge 1 commit into
matplotlib:mainfrom
lucaznch:fix-nearest-single-row-column-grid

Conversation

@lucaznch

Copy link
Copy Markdown
Contributor

PR summary

Closes #25699

Explicitly providing a single row and/or single column grid for pcolor(mesh) with nearest shading, silently produces no plot.

This happens because nearest shading interprets the X and Y coordinates as cell centers and then calculates, from neighboring centers, new X and Y coordinates for the cell corners. When a coordinate array contains only one center, its corresponding cell size cannot be determined from neighboring centers.

This PR adds a fallback that determines the directional cell size for a single center by taking half of its absolute value on either side. For a zero-valued center -/+ 0.5 is used instead. This allows the cells to be plotted. And a warning is emitted to notify the user.

AI Disclosure

Had AI help me understand parts of the logic of the interp_grid() function.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

Explicitly providing a single row and/or single column grid
for pcolor(mesh) with nearest shading, silently produces no plot.

This happens because nearest shading interprets the X and Y coordinates as
cell centers and then calculates, from neighboring centers, new X and Y
coordinates for the cell corners. When a coordinate array contains only
one center, its corresponding cell size cannot be determined from neighboring centers.

This commit adds a fallback that determines the directional cell size for a
single center by taking half of its absolute value on either side. For a
zero-valued center -/+ 0.5 is used instead. This allows the cells to be
plotted. And a warning is emitted to notify the user.
@timhoffm

timhoffm commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

As a rule, valid and intended usage patterns should not gernerate a warning, because that's annoying. A user must always have a way to write their code without warnings popping up. If we decide that single row is a valid use case, then we cannot plot and warn. For the concrete case we thus have the options:

  • not supporting single rows and instead raise with the suggestion to specify edges instead
  • decide that a specific size is a universally justifiable assumtion and use that without warning
  • add some user-customizable way to control the behavior. This could e.g. be an additional parameter either (somewhat ugly) just for opting into single-row handling, or better: configurable single row behavior:
    • 'auto': the above behavior
    • 'default': 'auto' and warn - that way users can set the behavior expicitly to 'auto' and thus the warning
    • float: a fixed number
    • array: different values per point

@lucaznch

Copy link
Copy Markdown
Contributor Author

Regarding a universal default for dX that would not need a warning, I'm unsure if a single fixed size would work well for the different types of coordinates that X can have have. I don't see an obvious default size that would be appropriate for all, so asking the user to supply the cell corners explicitly seems preferable.

Then I'm leaning towards the first option of not supporting single row/column grids.

@jklymak

jklymak commented Sep 30, 2026

Copy link
Copy Markdown
Member

Despite my previous comments, I wonder if this should be centred in the current axes viewport. The rationale being that someone may have already plotted something on the axes and we do not want the axes limits to change arbitrarily. Similarly that obviates concerns about datetimes.

This branch has not been deployed

No deployments
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.

[Bug]: pcolormesh/pcolor doesn't show the image of an array of (M*1) OR (1*M) when providing X and Y explicitly

4 participants