在imshow&中发布带有图像交叉日期线的问题。 cartopy

时间:2017-11-16 17:31:54

标签: python matplotlib cartopy

我正在尝试使用cartopy,matplotlib和imshow绘制等间距(纬度/经度)数据的方格。数据越过日期,我遇到了让地图正常工作的问题。

以下是我的问题的一个例子:

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


lat = np.arange(6000)*0.02 + (-59.99)
lon = np.arange(6000)*0.02 + (85.01)

dat = np.reshape(np.arange(6000*6000),[6000,6000])

tran = ccrs.PlateCarree()
proj = tran

plt.figure(figsize=(8,8))

ax = plt.axes(projection=proj)
print([lon[0],lon[-1],lat[0],lat[-1]])
ax.imshow(dat, extent=[lon[0],lon[-1],lat[0],lat[-1]],transform=tran,interpolation='nearest')
ax.coastlines(resolution='50m', color='black', linewidth=2)
ax.gridlines(crs=proj,draw_labels=True)
plt.show()

tran = ccrs.PlateCarree(central_longitude=180)
proj = tran

plt.figure(figsize=(8,8))

ax = plt.axes(projection=proj)
print([lon[0]-180,lon[-1]-180,lat[0],lat[-1]])
ax.imshow(dat, extent=[lon[0]-180,lon[-1]-180,lat[0],lat[-1]],transform=tran,interpolation='nearest')
ax.coastlines(resolution='50m', color='black', linewidth=2)
ax.gridlines(crs=tran,draw_labels=True)
plt.show()

第一张图产生了这张图片,在180E时切断: chopped at 180

第二个修复了地图问题,但网格刻度现在是错误的: 180 now zero

我已经尝试过重新投影,我认为(其中tran!= proj),但它似乎要么挂了,要么花了太长时间。

我基本上想要底部图像,但使用适当的标签。我将有更多的地理定位数据来过度绘图,所以我想正确地做,而不是现在似乎是一个黑客。

1 个答案:

答案 0 :(得分:0)

使用Cartopy,绘制地图交叉日期线始终具有挑战性。以下是绘制所需地图的代码。

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

# demo raster data
n1 = 300
m1 = 0.4
lat = np.arange(n1)*m1 + (-59.99)
lon = np.arange(n1)*m1 + (85.01)
dat = np.reshape(np.arange(n1*n1), [n1,n1])

cm_lon=180  # for central meridian

tran = ccrs.PlateCarree(central_longitude = cm_lon)
proj = tran

plt.figure(figsize=(8,8))
ax = plt.axes(projection=proj)

ext = [lon[0]-cm_lon, lon[-1]-cm_lon, lat[0], lat[-1]]
#print(ext)

ax.imshow(dat, extent=ext, \
              transform=tran, interpolation='nearest')

ax.coastlines(resolution='110m', color='black', linewidth=0.5, zorder=10)

# this draws grid lines only, must go beyond E/W extents
ax.gridlines(draw_labels=False, xlocs=[80,100,120,140,160,180,-180,-160,-140])

# this draw lables only, exclude those outside E/W extents
ax.gridlines(draw_labels=True, xlocs=[100,120,140,160,180,-160])

plt.show()

生成的地图:

output