import netCDF4 import wrf import numpy as np import matplotlib.pyplot import cartopy.crs as ccrs import cartopy.feature # 读取数据###############################################################开始 data_nc = netCDF4.Dataset(r"./data/WRF/wrfout_d01_2023-01-15_12%3A00%3A00") Start_Date = str(data_nc.START_DATE).replace('_', '\/\/') # 经纬度范围 lat_SIZE = wrf.getvar(data_nc, "XLAT").shape[0] lon_SIZE = wrf.getvar(data_nc, "XLONG").shape[0] print(lon_SIZE, lat_SIZE) lon_min = wrf.getvar(data_nc, "XLONG")[0][0].values lat_min = wrf.getvar(data_nc, "XLAT")[0][0].values lon_max = wrf.getvar(data_nc, "XLONG")[0][lon_SIZE-1].values lat_max = wrf.getvar(data_nc, "XLAT")[lat_SIZE-1][0].values # print(lat_min1.values, lon_min1.values, lat_max1.values, lon_max1.values) # 底图###################################################################开始 # 设置中文字体 matplotlib.pyplot.rcParams['font.sans-serif'] = ['SimHei'] fig = matplotlib.pyplot.figure(figsize=(4, 4), dpi=150) axes = matplotlib.pyplot.subplot(1, 1, 1, projection=ccrs.PlateCarree()) # 设定底图范围 # lon_min, lon_max, lat_min, lat_max = 104.1, 135.9, 13.18, 41.04 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) # 左下角文字 # 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:'+str(Start_Date)+'}$', 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(cartopy.feature.COASTLINE.with_scale('10m'), lw=0.4) # 添加坐标 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 = matplotlib.ticker.FixedLocator(np.arange(lon_min, lon_max, 2.5)) # 设置坐标轴范围和间隔 gl.ylocator = matplotlib.ticker.FixedLocator(np.arange(lat_min, lat_max, 2.5)) gl.xlabel_style = {'size': 4} # 修改经纬度字体大小 gl.ylabel_style = {'size': 4} # 底图###################################################################结束 fig_name = "./test_WRF_Grids.jpg" matplotlib.pyplot.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’}: 目前只有后端支持