0

I have asked similar question about plotting RGB array on normal axis. The idea is plotting one channel of RGB array and set the facecolor by the combination of RGB.

I wanna apply this method to axis with projection.

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

np.random.seed(100)

x = np.arange(10, 20)
y = np.arange(0, 10)
x, y = np.meshgrid(x, y)

img = np.random.randint(low=0, high=255, size=(10, 10, 3))

ax = plt.axes(projection=ccrs.PlateCarree())

mesh = ax.pcolormesh(x, y, img[:, :,0], facecolors=img.reshape(-1, 3)/255)
mesh.set_array(None)
plt.show()

However, I got this error because of None type.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[59], line 17
     14 ax = plt.axes(projection=ccrs.PlateCarree())
     16 mesh = ax.pcolormesh(x, y, img[:, :,0], facecolors=img.reshape(-1, 3)/255)
---> 17 mesh.set_array(None)
     18 plt.show()

File ~/miniconda3/envs/enpt/lib/python3.10/site-packages/cartopy/mpl/geocollection.py:29, in GeoQuadMesh.set_array(self, A)
     27 def set_array(self, A):
     28     # raise right away if A is 2-dimensional.
---> 29     if A.ndim > 1:
     30         raise ValueError('Collections can only map rank 1 arrays. '
     31                          'You likely want to call with a flattened array '
     32                          'using collection.set_array(A.ravel()) instead.')
     34     # Only use the mask attribute if it is there.

AttributeError: 'NoneType' object has no attribute 'ndim'
zxdawn
  • 825
  • 1
  • 9
  • 19
  • Clearly, we can see that `None` in `mesh.set_array(None)` causes the error. As such, it's not clear what you're trying to do. What are you trying to do, because it's not clear? – Trenton McKinney Aug 31 '23 at 20:38
  • @TrentonMcKinney Thanks, I have added the idea of the code. – zxdawn Aug 31 '23 at 20:44
  • You didn't read the more modern solution at https://stackoverflow.com/a/75950850/3394386 – Jody Klymak Sep 01 '23 at 00:29
  • This isn't a direct duplicate of the other question because that one was pure Matplotlib and this one uses Cartopy. However, since Cartopy v0.22, you can pass the RGB array directly to `pcolormesh` https://scitools.org.uk/cartopy/docs/latest/whatsnew/v0.22.html#features – RuthC Sep 02 '23 at 17:15

0 Answers0