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:
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