【跟着SCI学作图】Cartopy和Matplotlib fill_betweenx可视化nc数据

Cartopy和Matplotlib fill_betweenx可视化nc数据

在这里插入图片描述

01 引言:

最近读文献【African Lightning and its Relation to Rainfall and Climate Change in a Convection‐Permitting Mode】发现文中图表的排版布局非常好,故借鉴一下。懒得下载文中的数据,利用手头的数据进行了代替,故而有所差异。
在这里插入图片描述

02 代码如下:

# -*- encoding: utf-8 -*-
'''
@File    :   sci2.py
@Time    :   2022/06/17 15:38:10
@Author  :   HMX 
@Version :   1.0
@Contact :   kzdhb8023@163.com
'''

# here put the import lib
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import xarray as xr
import pandas as pd


def cm2inch(x,y): 
    return (x/2.54,y/2.54)


size1 = 10.5
fontdict = {'weight': 'bold','size':size1,'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'],
    }
    )

ds = xr.open_dataset(r'C:\Users\HMX\Desktop\VHRFC.nc')
proj = ccrs.PlateCarree()
region = [-20,60,-40,40]
norm = mpl.colors.Normalize(vmin=0, vmax=100)#将颜色映射到 vmin~vmax 之间
fig = plt.figure(figsize=cm2inch(16,7))
ax0 = fig.add_axes([0.07,0.1,0.03,.85])
ax1 = fig.add_axes([0.25,0.1,0.4,.85], projection=ccrs.PlateCarree())
ax2 = fig.add_axes([0.7,0.1,0.25,0.85])
ax1.add_feature(cfeature.COASTLINE.with_scale('10m'),zorder = 10)
ds = ds.VHRFC_LIS_FRD
ds = ds.sel(Longitude = slice(-20,60), Latitude = slice(-40,40))
img = ds.plot.contourf(transform = proj,cmap = 'Reds',ax = ax1,add_colorbar=False,norm = norm,add_xlabel = False,add_ylabel = False)
fig.colorbar(img,cax = ax0,extend='max',label = 'Flash density (flashes km$^{-2}$ yr$^{-1}$)')
ax1.set_ylabel([],color = 'none')
ax1.set_extent(region,crs = proj)
ax1.set_xticks(np.arange(region[0], region[1] + 1, 10), crs = proj)
ax1.set_yticks(np.arange(region[-2], region[-1] + 1,10), crs = proj)

# 首先计算均值和上下四分位
ds2 = xr.open_dataarray(r'C:\Users\HMX\Desktop\VHRF_land.nc')
ds2 = ds2.sel(Longitude = slice(-20,60))
q1s, q4s, mus = [], [], []
x,y = ds2.values.shape
for i in range(x):
    data = ds2.values[:,i]
    res = pd.DataFrame({'cg':data})
    res = res[res.cg>0]
    mu = res.describe().values[1][0]
    mus.append(mu)
    sd = np.std(data)
    n = len(ds)
    q1, q4 =res.describe().values[4][0], res.describe().values[6][0]
    q1s.append(q1)
    q4s.append(q4)


ax2.fill_betweenx(ds2.Latitude.values,q1s,q4s, alpha=0.2,label = ' 25th to 75th',color = 'r')
ax2.plot(mus,ds2.Latitude.values,color = 'r')
ax2.set_xlim(-20,60)
ax2.set_ylim(-40,40)
ax2.set_xticks(np.arange(0,60+ 1, 20))
ax2.set_xlim(-5,45)
ax2.set_yticklabels([])
fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax2.transAxes)
plt.savefig(r'.\sci2.png',dpi = 600)
plt.show()

03 结果如下:

在这里插入图片描述

​如果对你有帮助的话,请‘点赞’、‘收藏’,‘关注’,你们的支持是我更新的动力。
欢迎关注公众号【森气笔记】。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值