使用 Cartopy 从数据中屏蔽海洋或陆地
Mask Ocean or Land from data using 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()
上面的代码绘制了这样的数据:
我想屏蔽这片土地。
为了掩蔽土地面积,使用底图会更容易。
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
我浏览了 cartopy 文档并发现了名为 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
我想从全球海表温度数据中屏蔽陆地区域。我正在使用 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()
上面的代码绘制了这样的数据:
我想屏蔽这片土地。
为了掩蔽土地面积,使用底图会更容易。
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
我浏览了 cartopy 文档并发现了名为 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