""" WRF 绘制海平面气压场 Sea Level Pressure 20221225 source:http://www.manongjc.com/detail/60-cqsbwjinsqxsfob.html """ import matplotlib.pyplot as plt import numpy as np import netCDF4 as nc from matplotlib.font_manager import FontProperties from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER import matplotlib.ticker as mticker import matplotlib as mpl import cartopy.crs as ccrs import cartopy.feature as cf from wrf import to_np, getvar, interplevel, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords, vertcross, smooth2d, CoordPair, GeoBounds,interpline import cmaps from matplotlib.cm import get_cmap plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体 fig = plt.figure(figsize=(4, 4), dpi=150) axes = plt.subplot(1, 1, 1, projection=ccrs.PlateCarree()) lon_min, lon_max, lat_min, lat_max = 103, 127.5, 12, 28 axes.set_extent([lon_min, lon_max, lat_min, lat_max], crs=ccrs.PlateCarree()) # 添加字符############################################################### axes.set_title('海平面气压$\mathbf{(hPa)}$', fontsize=5, loc='left', y=0.98) axes.set_title('$\mathbf{WRF V4.0}$', fontsize=4, loc='right', y=0.98) # fig.text(12, 125, 'WRF V4.0', fontsize=7, color='b', transform=axes.transAxes) #添加标题 #title # axes.text(.08,1.1,"Base Charts in Python Exercise 02: Bar Charts", # transform = axes.transAxes,color='k',ha='left',va='center',size=18,fontweight='extra bold') # #subtitle # axes.text(.01,1.02,"processed bar charts with seaborn.histplot()", # transform = axes.transAxes,color='k',ha='left',va='center',size=9,fontweight='bold') # 左下角文字 axes.text(0.01, -0.02, '$\mathbf{Valid:2022-12-17\/\/12:00:00}$', transform=axes.transAxes, ha='left', va='center', fontsize=4, color='black', fontweight='bold') # 右下角文字 axes.text(.99, -0.02, '$\mathbf{Initial:2022-12-17\/\/12:00:00}$', transform=axes.transAxes, ha='right', va='center', fontsize=4, color='black', fontweight='bold') # 右下角文字 axes.text(.99, 0.02, '$\mathbf{\u00A9BYP}$', transform=axes.transAxes, ha='right', va='center', fontsize=4, color='black', fontweight='bold') ################################################################ # 添加地物############################################################### axes.add_feature(cf.COASTLINE.with_scale('10m'), lw=0.4) # axes.add_feature(cf.LAKES.with_scale('110m'), linewidth=1, color='k') # axes.add_feature(cf.RIVERS.with_scale('110m'), lw=0.4) # axes.add_feature(cf.LAND.with_scale('110m')) # axes.add_feature(cf.OCEAN.with_scale('110m')) # axes.add_feature(cf.BORDERS.with_scale('10m'), lw=0.4) # axes.add_feature(cf.STATES.with_scale('110m')) ################################################################ # 添加坐标############################################################### gl = axes.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linestyle=":", linewidth=0.3, x_inline=False, y_inline=False, color='gray') gl.top_labels, gl.bottom_labels, gl.right_labels, gl.left_labels = False, False, False, False # gl.xformatter = LONGITUDE_FORMATTER # 使横坐标转化为经纬度格式 无效 # gl.yformatter = LATITUDE_FORMATTER gl.xlocator = mticker.FixedLocator(np.arange(lon_min, lon_max, 2.5)) # 设置坐标轴范围和间隔 gl.ylocator = mticker.FixedLocator(np.arange(lat_min, lat_max, 2.5)) gl.xlabel_style = {'size': 4} # 修改经纬度字体大小 gl.ylabel_style = {'size': 4} ################################################################ # 读取数据############################################################### data_nc = nc.Dataset('../Reanalysis II/data/WRF/wrfout_d01_2022-12-17_12_00_00') # Get the sea level pressure slp = getvar(data_nc, "slp") # Smooth the sea level pressure since it tends to be noisy near the # mountains smooth_slp = smooth2d(slp, 3, cenweight=4) # Get the latitude and longitude points lats, lons = latlon_coords(slp) # Get the cartopy mapping object cart_proj = get_cartopy(slp) # Make the contour outlines and filled contours for the smoothed sea level # pressure. plt.contour(to_np(lons), to_np(lats), to_np(smooth_slp), 10, colors="black", linewidths=0.2, transform=ccrs.PlateCarree()) plt.contourf(to_np(lons), to_np(lats), to_np(smooth_slp), 10, transform=ccrs.PlateCarree(), cmap=get_cmap("jet")) # # Add a color bar # plt.colorbar(ax=axes, shrink=.98) # # # Set the map bounds # axes.set_xlim(cartopy_xlim(smooth_slp)) # axes.set_ylim(cartopy_ylim(smooth_slp)) # Add the gridlines # axes.gridlines(color="grey", linestyle="dashed", linewidths=0.01) ################################################################ # plt.show() fig_name = "./test.jpg" plt.savefig(fig_name, dpi=512, # 分辨率,每英寸的点数 512 bbox_inches='tight', pad_inches=0.1, # (默认: 0.1)所保存图形周围的填充量 transparent=True, facecolor='auto', # 默认 auto edgecolor='r', # 默认 auto # papertype='letter', # 3.6之后不支持了。纸张大小,仅支持postscript输出。取值范围为: # {'letter', 'legal', 'executive', 'ledger', 'a0' - 'a10', 'b0' - 'b10'}。默认值为None。 orientation='portrait') # {‘landscape,’ ‘portrait’}: 目前只有后端支持