############################################
fig = plt.figure(figsize=(24,13),dpi=600)
proj = ccrs.PlateCarree(central_longitude=180)##设置一个圆柱投影坐标,中心经度180°E
extent = [100,180,0,40]##设置地图边界范围
xticks=np.arange(extent[0],extent[1]+1,30)
yticks=np.arange(extent[2],extent[3]+1,30)
plt.subplots_adjust(wspace=0.25, hspace=0.15)#wspace、hspace左右、上下的间距
font= {'family' : 'Times New Roman','weight' : 'bold','size' : 21}#设置横纵坐标的名称以及对应字体格式family'
for i in range(9): #画3行3列排布的子图
###################################以下为地图投影以及坐标轴的设置
ax = fig.add_subplot(3,3,i+1,projection=ccrs.PlateCarree(central_longitude=180))
#地图相关设置,包括边界,河流,海岸线,坐标
ax.set_extent(extent,crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
#添加陆地并且陆地部分全部填充成浅灰色
ax.add_feature(cfeature.LAND.with_scale('10m'),color='lightgray')
ax.add_feature(cfeature.LAKES, alpha=0.5)
#横纵坐标设置
ax.set_xticks(xticks, crs=ccrs.PlateCarree())
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter()
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
fg=ax.contourf(lon,lat,dif[i+0,:,:],np.linspace(-2.5,2.5,21), zorder=0, extend =
'both', transform=ccrs.PlateCarree(),cmap=cmaps.BlueWhiteOrangeRed)
#标注模式名称
ax.text(-2, 35, name[i+0],
color = "k", style = "italic", weight = "light", font=font,
verticalalignment='center', horizontalalignment='right',rotation=0)
#添加网格线
# plt.grid(b='true',which='major',axis='both',color='k', linestyle='--',linewidth=2)
#刻度线设置
xmajorLocator = MultipleLocator(20)#先定义xmajorLocator,再进行调用
ax.xaxis.set_major_locator(xmajorLocator)#x轴最大刻度
xminorLocator = MultipleLocator(10)
ax.xaxis.set_minor_locator(xminorLocator)#x轴最小刻度
ymajorLocator = MultipleLocator(10)
ax.yaxis.set_major_locator(ymajorLocator)#y轴最大刻度
yminorLocator = MultipleLocator(5)
ax.yaxis.set_minor_locator(yminorLocator)#y轴最小刻度
#最大刻度、最小刻度的刻度线长短,粗细设置
ax.tick_params(which='major', length=12,width=1,color='k')#最大刻度长度,宽度设置,
ax.tick_params(which='minor', length=7,width=0.8,color='k')#最小刻度长度,宽度设置
ax.tick_params(which='both',bottom=True,top=False,left=True,labelbottom=True,
labeltop=False)
plt.rcParams['xtick.direction'] = 'out' #将x轴的刻度线方向设置向内或者外
#设置坐标刻度值的大小以及刻度值的字体
plt.tick_params(labelsize=16)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
# 9个子图共用colorbar位置
position = fig.add_axes([0.13, 0.42, 0.36, 0.07 ]) #位置[左,下,右,上]
cb = plt.colorbar(fg, cax=position,orientation='vertical')
cb.ax.tick_params(length=2, labelsize='medium')#length为刻度线的长度
font = {'family' : 'Times New Roman','weight' : 'normal','size' : 16}
cb.set_label('future change /hPa', fontdict=font) #colorbar 单位以及字体大小
# colorbar 指定刻度值以及刻度值个数
import matplotlib.ticker as ticker
tick_locator = ticker.MaxNLocator(nbins=5) # colorbar上的刻度值个数
cb.locator = tick_locator
# cb.set_ticks([-0.04,-0.02, 0, 0.02, 0.04])
# cb.set_ticklabels(['-0.04', '-0.02', '0', '0.02','0.04'])
cb.update_ticks()
python 使用循环画多个子图
最新推荐文章于 2025-04-29 15:28:57 发布