【跟着SCI学作图】Matplotlib pcolormesh可视化nc数据

文摘   教育   2023-01-27 22:24   江苏  


01 引言:

今天接着复现【Future increases in Arctic lightning and fire risk for permafrost carbon】的图表,主要是xarray读取nc数据,然后通过Matplotlib的pcolormesh可视化nc数据

论文中提供的数据如下图所示:

数据下载地址:

【https://www.nature.com/articles/s41558-021-01011-y#Sec17】

02 代码如下:

# -*- encoding: utf-8 -*-'''@File    :   png.py@Time    :   2023/01/27 21:34:38@Author  :   HMX @Version :   1.0@Contact :   kzdhb8023@163.com'''# here put the import lib
import xarray as xrimport osimport cartopy.crs as ccrsimport cartopy.feature as cfeatureimport cmapsimport matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npfrom cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatterimport cmaps

# 字体size1 = 10.5fontdict = {'weight': 'bold','size':size1,'color':'k','family':'SimHei'}mpl.rcParams.update( { 'text.usetex': False, 'font.family': 'stixgeneral', 'mathtext.fontset': 'stix', "font.family":'serif', "font.size": size1, "mathtext.fontset":'stix', "font.serif": ['Times New Roman'], } )

# 读取数据os.chdir(r'E:\CODE\work\plot7\png5\data')nc = '41558_2021_1011_MOESM14_ESM.nc'ds = xr.open_dataset(nc)print(ds)ds = ds['OTD']

# 可视化proj=ccrs.PlateCarree()fig,ax = plt.subplots(1, 1,figsize=(8,4),dpi=100, subplot_kw={'projection': proj})extent = [-170,-140,55,70]
ax.add_feature(cfeature.COASTLINE.with_scale('110m'), linewidth=0.5, zorder=2,color = 'k')# 添加海岸线ax.add_feature(cfeature.LAND)#添加陆地ax.set_extent(extent, crs=ccrs.PlateCarree())ax.set_xticks(np.arange(extent[0], extent[1] + 1, 10), crs = proj)ax.set_yticks(np.arange(extent[-2], extent[-1] + 1,5), crs = proj)ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=False))ax.yaxis.set_major_formatter(LatitudeFormatter())
lev=np.arange(0,0.16,0.001)lon,lat = np.meshgrid(ds['lon'],ds['lat'])cf=ax.pcolormesh(lon,lat,ds,transform=ccrs.PlateCarree(), cmap=cmaps.MPL_BuPu, vmin=0, vmax=0.15)

# colorbarplt.subplots_adjust(right=0.84)ax2 = fig.add_axes([0.875,0.135,0.02,0.72])b=plt.colorbar(cf,shrink=0.93,orientation='vertical',extend='neither',pad=0.05,aspect=30,ticks=np.arange(0,0.2,0.05),cax=ax2)b.ax.set_ylabel('OTD FR(#km${^2}$mo${^2}$)')
plt.savefig(r'5.png',dpi = 600)plt.show()

03 结果如下:

欢迎私交流学习


戳这里关注我

请点赞、在看、关注,你们的支持是我更新的动力。

森气笔记
记录分享森林气象学相关的Python GEE Arcgis QGIS Matlab等学习笔记