网上的白化教程各式各样,我也是自己从零开始慢慢摸索,这也算是篇记录自己心路历程的整合吧,代码也是来源网上,在这只做一个使用解说的效果。
首先先看一下没有白化的样例代码和图:
from netCDF4 import Dataset as nc
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
if __name__=='__main__':
pth='air.2m.mon.mean.nc'
f=nc(pth)
t=f.variables['air'][:]
lon=f.variables['lon'][:]
lat=f.variables['lat'][:]
t=t[0,0,:,:]
t=t-273.15
fig=plt.figure(figsize=(11.5,4.5))
ax=fig.add_subplot(1,1,1,projection=ccrs.PlateCarree())
h=ax.contourf(lon,lat,t,cmap='jet',transform=ccrs.PlateCarree())
ax.coastlines()
plt.colorbar(h,location='right')
g=ax.gridlines(draw_labels=['left','bottom'])
g.xlines=False
g.ylines=False
接下来我们开始利用shp2clip()函数对中国地区以外的白化,即屏蔽其他地区
先放出shp2clip()函数的代码,稍后说明怎么使用:
def shp2clip(originfig, ax, shpfile, regionlist): #白化函数
sf = shapefile.Reader(s