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

GeoQuadMesh.set_array cannot handle None #2045

Closed
kconnour opened this issue May 16, 2022 · 4 comments · Fixed by #2166
Closed

GeoQuadMesh.set_array cannot handle None #2045

kconnour opened this issue May 16, 2022 · 4 comments · Fixed by #2166

Comments

@kconnour
Copy link

Description

I have an rgb array of pixels that I'd like to project onto a globe to simulate what a spacecraft saw. To do this, I'm using pcolormesh. This requires me (as far as I know) to use set_array(None) in order to plot the rgb values (if I remove that part, matplotlib defaults back to the viridis colormap), which fails in the GeoQuadMesh.set_array method because that method expects A to be an ndarray. When I remove the check that A has dimensionality of at least 1, everything works as expected.

Code to reproduce

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

# Setup the figure
rmars = 3400
fig = plt.figure(figsize=(6, 6), facecolor='k')
globe = ccrs.Globe(semimajor_axis=rmars * 1e3, semiminor_axis=rmars * 1e3)
projection = ccrs.NearsidePerspective(central_latitude=-10, central_longitude=250, satellite_height=6400 * 10 ** 3, globe=globe)
transform = ccrs.PlateCarree(globe=globe)
globe_ax = plt.axes(projection=projection)

# Attempt to plot a dummy set of rgb values at a dummy set of latitude/longitude points
globe_ax.pcolormesh(np.array([[1, 2], [3, 4]]), np.array([[1, 2], [3, 4]]), [[1]], color=[[0.2, 0.5, 0.6]], linewidth=0, edgecolors='none', rasterized=True, transform=transform).set_array(None)

Traceback

Traceback (most recent call last):
  File "/home/me/repos/myrepo/graphics/standard_products.py", line 265, in <module>
    globe_ax.pcolormesh(np.array([[1, 2], [3, 4]]), np.array([[1, 2], [3, 4]]), [[1]], color=[[0.2, 0.5, 0.6]], linewidth=0, edgecolors='none', rasterized=True,
  File "/home/me/myrepo/venv/lib/python3.9/site-packages/cartopy/mpl/geocollection.py", line 29, in set_array
    if A.ndim > 1:
AttributeError: 'NoneType' object has no attribute 'ndim'

Full environment definition

Operating system

Ubuntu 22.04

Cartopy version

0.20.2

conda list

I don't use conda

pip list

matplotlib 3.5.2
numpy 1.22.3
@dopplershift
Copy link
Contributor

Looks like matplotlib does explicitly support set_array(None), so we should too.

@nvogtvincent
Copy link

Hi, is there any update on this? I've run into this issue several times because, at present, there is no way to plot an RGBA image using pcolormesh with cartopy (see here).

@kconnour
Copy link
Author

kconnour commented Apr 7, 2023

@nvogtvincent I've heard nothing about this issue since raising it. However, with the recently released matplotlib v3.7.0, you no longer need to use set_array(None) with pcolormesh; it now natively supports RGB(A) arrays. I haven't tested it with Cartopy yet though.

@rcomer
Copy link
Member

rcomer commented Apr 24, 2023

I opened #2166 to try to get pcolormesh working directly with RGB(A), but it turned out to be way more complicated than I thought and I don't know how soon we will complete it.

I suspect all that would be needed to support this workaround would be to skip the array-specific checks in set_array if A is None. That seems like an ideal task for a new contributor if any of you want to try it and, if it works, submit a pull request @kconnour, @nvogtvincent, @julietbravo. There is a brief guide to contributing here:
https://scitools.org.uk/cartopy/docs/latest/contribute.html#contribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants