使用cartopy pcolormesh

时间:2018-02-01 15:50:49

标签: python-2.7 matplotlib matplotlib-basemap cartopy

我正在尝试使用随数据集提供的不规则lat / lon数组在地图上绘制RGB数组。为此我正在使用pcolormesh,如here所述。 问题是,当它越过日期线(180子午线)时,cartopy pcolormesh似乎无法呈现数据,或者至少我找不到解决方法。下面的脚本重新生成了问题:

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

生成此图: enter image description here 有没有人知道如何使用cartopy或底图工具包或任何其他工具解决这个问题?

PS:我在Windows x64上使用python 2.7,库是最新的更新

0 个答案:

没有答案