2

I am trying to plot an RGB array over a map using irregular lat/lon array that are provided with the dataset. For this purpose I am using pcolormesh as discussed here. The problem is that cartopy pcolormesh seems to be unable to render the data when it is crossing the dateline (180 meridian), or at least I couldn't find a workaround. Script below regenerates the issue:

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

# RGBA image
r = np.array([[ 0.27359769,  0.27359769,  0.27359769,  0.27359769],
       [ 0.27359769,  0.67702749,  0.85702749,  0.27359769],
       [ 0.27359769,  0.85702749,  0.67702749,  0.27359769],
       [ 0.27359769,  0.67702749,  0.85702749,  0.27359769],
       [ 0.27359769,  0.85702749,  0.67702749,  0.27359769],
       [ 0.27359769,  0.27359769,  0.27359769,  0.27359769]])

g = np.array([[ 0.45662555,  0.3562555 ,  0.45662555,  0.3562555 ],
       [ 0.3562555 ,  0.13968855,  0.13968855,  0.45662555],
       [ 0.45662555,  0.13968855,  0.13968855,  0.3562555 ],
       [ 0.3562555 ,  0.13968855,  0.13968855,  0.45662555],
       [ 0.45662555,  0.13968855,  0.13968855,  0.3562555 ],
       [ 0.3562555 ,  0.45662555,  0.3562555 ,  0.45662555]])

b = np.array([[ 0.83175885,  0.5175885 ,  0.8175885 ,  0.5615689 ],
       [ 0.5175885 ,  0.32744445,  0.32744445,  0.83175885],
       [ 0.83175885,  0.32744445,  0.32744445,  0.5175885 ],
       [ 0.5175885 ,  0.32744445,  0.32744445,  0.83175885],
       [ 0.83175885,  0.32744445,  0.32744445,  0.5175885 ],
       [ 0.5175885 ,  0.83175885,  0.5175885 ,  0.83175885]])
rgb = np.dstack((r,g,b))

mask = np.array([[ True,  True ,  True ,  True ],
       [ True,  True ,  True ,  True],
       [ True,  True ,  True ,  True],
       [ True,  True ,  True ,  True],
       [ False , False, False, False],
       [ True,  True ,  True ,  True]])

rgba = np.dstack([rgb,mask.astype('int')])

# Generate lat lon arrays
lons,lats = np.meshgrid(np.linspace(-140,-180,5),np.linspace(85,65,7))
i=0
for j in range(len(lons)):
    lons[j] -= i
    i+=5
for j in range(len(lats.T)):
    lats[:,j] -= i
    i-=2
lons[lons<-180] +=360

# Plot
plt.figure(figsize=(10,5))
ax1 = plt.subplot(121)
ax1.imshow(rgba)
ax1.set_title('RGBA')

colorTuple = rgba.reshape((rgb.shape[0]*rgba.shape[1],rgba.shape[2]))
proj = ccrs.PlateCarree(central_longitude=180)
ax2 = plt.subplot(222,projection=proj)
pcol = ax2.pcolormesh(lons,lats,r,
                     color = colorTuple,
                     transform=ccrs.PlateCarree(),
                     linewidth=0)
pcol.set_array(None)
ax2.coastlines()
ax2.set_global()
ax2.set_title('central_longitude=180')

proj = ccrs.PlateCarree()
ax3 = plt.subplot(224,projection=proj)
pcol = ax3.pcolormesh(lons,lats,r,
                     color = colorTuple,
                     transform=ccrs.PlateCarree(),
                     linewidth=0)
pcol.set_array(None)
ax3.coastlines()
ax3.set_global()
ax3.set_title('central_longitude=0')

plt.tight_layout()
plt.show()

Which generates this plot: enter image description here Does anyone know how to work around this problem using cartopy or basemap toolkit or any other tools?

PS: I am using python 2.7 on windows x64, and libraries are all the most recent updates

Monobakht
  • 193
  • 1
  • 11
  • I think I remember having seen a similar issue. Did you check if this hasn't been asked somewhere already, if not on SO, maybe in the respective issue trackers? – ImportanceOfBeingErnest Feb 01 '18 at 16:26
  • @ImportanceOfBeingErnest I couldn't find a similar issue anywhere. There is this question which is slightly similar to my problem, but not quite the same: (https://stackoverflow.com/questions/43984077/cartopy-pcolormesh-with-re-normalized-colorbar) . Do you know how should I raise the issue on issue tracker? – Monobakht Feb 01 '18 at 16:58
  • So the problem you link to got solved by changing the masking. Is this similar here, what would happen if you leave out the masked area? – ImportanceOfBeingErnest Feb 01 '18 at 19:52
  • @ImportanceOfBeingErnest No it is not caused because of masking. You can simply replace rgba by rgb in the code above to plot the non-masked array. The result is still the same. – Monobakht Feb 02 '18 at 09:14
  • 1
    [This is the question I was referring to](https://stackoverflow.com/questions/46527456/preventing-spurious-horizontal-lines-for-ungridded-pcolormesh-data). – ImportanceOfBeingErnest Feb 02 '18 at 10:04
  • I guess a possible solution is then given in [this follow-up question](https://stackoverflow.com/questions/46548044/why-does-pcolor-with-masked-arrays-fill-undesired-quadrangles-when-projected-in). – ImportanceOfBeingErnest Feb 02 '18 at 10:11
  • Have a look if [this](https://stackoverflow.com/q/48257845/2454357) helps you. – Thomas Kühn Feb 02 '18 at 13:38
  • Thanks @ImportanceOfBeingErnest for the links. I had come across those questions, but still couldn't solve the problem. Anyway, I ended up splitting the data into east and west subsets and plotted each subset individually. – Monobakht Feb 06 '18 at 15:01

0 Answers0