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

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

01 引言:

今天接着复现【Future increases in Arctic lightning and fire risk for permafrost carbon】的图表,主要是xarray读取nc数据,然后通过Matplotlib的pcolormesh可视化nc数据。
在这里插入图片描述
论文中提供的数据如下图所示:
【跟着SCI学作图】Matplotlib pcolormesh可视化nc数据_第1张图片

数据下载地址:
【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 :   [email protected]
'''
# here put the import lib

import xarray as xr
import os
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cmaps
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
import cmaps

# 字体
size1 = 10.5
fontdict = {'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)


# colorbar
plt.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 结果如下:

【跟着SCI学作图】Matplotlib pcolormesh可视化nc数据_第2张图片

以上就是本期推文的全部内容了,如果对你有帮助的话,请‘点赞’、‘收藏’,‘关注’,你们的支持是我更新的动力。

你可能感兴趣的:(python基础绘图,python,matplotlib可视化,python,matplotlib,numpy)