使用Cartopy从数据中屏蔽海洋或陆地

时间:2016-04-10 04:33:58

标签: python matplotlib cartopy

我想在全球范围内屏蔽陆地温度数据。我正在使用Cartopy绘制数据。

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from netCDF4 import Dataset

f = Dataset('sst.mnmean.nc')
sst = f.variables['sst'][0,:,:]
lats = f.variables['lat'][:]
lons = f.variables['lon'][:]

ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plot = ax.contourf(lons, lats, sst, 60, transform=ccrs.PlateCarree())
cb = plt.colorbar(plot)
plt.show()

以上代码绘制如下数据:
enter image description here

我想掩盖这片土地。

2 个答案:

答案 0 :(得分:2)

我浏览了手册文档并遇到了名为add_feature的方法。代码如下:

import numpy as np
import matplotlib.pyplot as plt
import cartopy as cart
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset

f = Dataset('sst.mnmean.nc')
sst = f.variables['sst'][0,:,:]
lats = f.variables['lat'][:]
lons = f.variables['lon'][:]

ax = plt.axes(projection=cart.crs.PlateCarree())
ax.coastlines()
ax.add_feature(cart.feature.LAND, zorder=100, edgecolor='k')
ax.set_global()
plot = ax.contourf(lons, lats, sst, 60, transform=cart.crs.PlateCarree())
cb = plt.colorbar(plot)
plt.show()

情节现在看起来像this。 要掩盖海洋,请将cart.feature.LAND更改为cart.feature.OCEAN

答案 1 :(得分:0)

对于屏蔽土地面积,使用底图会更容易。

from mpl_toolkits.basemap import Basemap
map = Basemap(projection='mill',lon_0=180) # create projection
....                                       # whatever processing needed
map.fillcontinents(color='coral')          # mask land mass

See basemap example here