cartopy icon indicating copy to clipboard operation
cartopy copied to clipboard

GeoQuadMesh.set_array cannot handle None

Open kconnour opened this issue 2 years ago • 1 comments

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

kconnour avatar May 16 '22 22:05 kconnour

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

dopplershift avatar May 17 '22 22:05 dopplershift

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).

nvogtvincent avatar Apr 06 '23 13:04 nvogtvincent

@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.

kconnour avatar Apr 07 '23 21:04 kconnour

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

rcomer avatar Apr 24 '23 09:04 rcomer