绘图函数封装

绘制带有地理底图时调用。

默认添加海岸线,湖泊,设置地图边界及X,Y轴经纬度刻度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import cartopy.mpl.ticker as cticker

def contour_map(fig,img_extent,spec1,spec2):
fig.set_extent(img_extent, crs=ccrs.PlateCarree())
fig.add_feature(cfeature.COASTLINE.with_scale('50m'))
fig.add_feature(cfeature.LAKES, alpha=0.5)
fig.set_xticks(np.arange(leftlon,rightlon+spec1,spec1), crs=ccrs.PlateCarree())
fig.set_yticks(np.arange(lowerlat,upperlat+spec2,spec2), crs=ccrs.PlateCarree())
lon_formatter = cticker.LongitudeFormatter()
lat_formatter = cticker.LatitudeFormatter()
fig.xaxis.set_major_formatter(lon_formatter)
fig.yaxis.set_major_formatter(lat_formatter)

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#创建图像
fig = plt.figure(figsize=(12,8))
f_ax1 = fig3.add_axes([0.1, 0.1, 0.5, 0.5],projection = proj)
#设置边界
leftlon, rightlon, lowerlat, upperlat = (130,360,-20,70)
img_extent = [leftlon, rightlon, lowerlat, upperlat]
#调用函数
contour_map(f3_ax1,img_extent,30,30)
#绘图
cf1 = f_ax1.contourf(lon,lat,r, zorder=0, levels =np.arange(-1,1.1,0.1) , extend = 'both',transform=ccrs.PlateCarree(), cmap=plt.cm.RdBu_r)
cf2 = f_ax1.contourf(lon,lat, p, [0,0.05,1] ,
zorder=1,hatches=['...', None],colors="none", transform=ccrs.PlateCarree())
#绘制色标
position=fig3.add_axes([0.2, 0.08, 0.35, 0.025])
fig3.colorbar(cf1,cax=position,orientation='horizontal',format='%.2f',)
#显示
plt.show()

输出图形如下:

image-20200702161610554