Cartopy set_extent 不起作用

问题描述 投票:0回答:2

我正在尝试使用 Mateplotlib cartopy 来绘制下面的图。这是我正在使用的代码。

import cartopy.crs as ccrs

fig=plt.figure(figsize=(15,11))
ax = plt.subplot(111, projection=ccrs.PlateCarree(central_longitude=0))
mm =   ax.contourf(new_lon[:],lat_post,new_aa[0,:,:],transform=ccrs.PlateCarree(central_longitude=0))

ax.coastlines(resolution='10m');
ax.stock_img();
# the following two lines increaes the vertical distance between the title and the upper tick.
from matplotlib import rcParams
rcParams['axes.titlepad']=20
# drawing the longitude and latitude ticks.
gl = ax.gridlines(crs=ccrs.PlateCarree(central_longitude=0), draw_labels=True,linewidth=2, color='gray', alpha=0.5, linestyle='--')

然而,一旦我添加以下代码 set_extent

ax.set_extent([np.min(new_lon),np.max(new_lon),np.min(lat_post)
,np.max(lat_post)])

数字变成这样

python python-2.7 matplotlib cartopy
2个回答
3
投票

看看你的原始地图,看起来在 0 经度处有一条接缝,所以我猜测

np.min(new_lon)
是 0,
np.min(new_lon)
是 360。如果你将其与
set_extent()
一起使用,你会得到一个真正的本初子午线处的窄带。我想如果你这样做
set_extent([-180, 180, ,np.min(lat_post), np.max(lat_post)]
,效果会更好。

在这种情况下,我能想到以编程方式实现它的唯一方法是:

lon_bounds = new_lon[:]  # copy
lon_bounds[lon_bounds > 180] -= 360
ax.set_extent([np.min(lon_bounds), np.max(lon_bounds), np.min(lat_post), np.max(lat_post)])

1
投票

set_extent 的工作原理是给出与中心经度和纬度的偏差。例如,如果中心经度是100E,那么

proj = ccrs.PlateCarree(central_longitude=100)

ax.set_extent([-50,50,-30,30], crs=proj)

将生成经度 50E 至 150E、纬度 30S 至 30N 的地图。

© www.soinside.com 2019 - 2024. All rights reserved.