Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Skip any nan coordinates in quadmesh #54

Merged
merged 1 commit into from
Dec 24, 2023

Conversation

greglucas
Copy link
Contributor

Even though the coordinates of quadmesh are meant to be finite, a transform may make them nonfinite. This adds a check to avoid the nan quads in the quadmesh rendering routine.

This fixes an issue in Cartopy+mplcairo
xref: SciTools/cartopy#2305

It would be nice if the backend spec (essentially the docstrings of the base methods in RendererBase) specified clearly which methods need to support nans, and for which inputs.

I agree, and perhaps even try to add some tests on that assertion, not just docstrings if we could. Do you want me to try and implement a test here, or look into adding something upstream in matplotlib?

Even though the coordinates of quadmesh are meant to be finite,
a transform may make them nonfinite. This adds a check to
avoid the nan quads in the quadmesh rendering routine.
@greglucas
Copy link
Contributor Author

Here is a quick MPL-only example demonstrating the bad quads get propagated to lower values of x instead of just the quad with the bad coordinate.

Without this patch, and with (fully filled color).
quadmesh_nan_bad
quadmesh_nan_good

import matplotlib.pyplot as plt
import matplotlib
import numpy as np

matplotlib.use("module://mplcairo.macosx")

fig, ax = plt.subplots()

x = np.linspace(-20, 20)
y = np.linspace(-20, 20)
xx, yy = np.meshgrid(x, y)

mesh = ax.pcolormesh(xx, yy, xx[:-1, :-1], shading='flat', cmap='viridis')
mesh._coordinates[30, 30, 0] = np.nan
plt.show()

@anntzer
Copy link
Collaborator

anntzer commented Dec 24, 2023

Thanks for the patch.

Do you want me to try and implement a test here, or look into adding something upstream in matplotlib?

The test should go to matplotlib: mplcairo picks up the matplotlib test suite and runs it while replacing the agg backend by itself (see run-mpl-test-suite.py).

@anntzer anntzer merged commit ea75c4f into matplotlib:main Dec 24, 2023
7 of 15 checks passed
@greglucas greglucas deleted the quadmesh-nan-coordinates branch December 24, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants