python气象绘图速成_Python气象数据处理与绘图:四分位时间序列图

本文介绍了如何使用Python绘制时间序列图,特别是包含四分位范围和百分位数阴影的图表。通过自定义函数`deftsplot`,可以方便地在图表中展示数据的波动性和趋势。该函数接受时间序列数据,计算指定百分位数的上下限,并填充阴影区域。示例中展示了如何应用此函数绘制历史气象数据的平均值及其四分位范围。此外,还提供了代码实现细节和参数说明。
摘要由CSDN通过智能技术生成

介绍

在文献中能经常看到一个时间序列图(横坐标为时间,纵坐标为变量)会有阴影覆盖(一般表现为淡一些的颜色),这样的图上下为25%-75%的范围。可以让人一眼看出数据随时间变化以及数据的波动性,近几年用的越来越多,所以也做了一些努力来还原这种图。

比如

7df5f36c3bf9

Global mean temperature anomaly and its decadal trend in CMIP6 models in response to different radiative forcings,The shaded area indicates the likely range (17 to 83% percentile).

看图中historical为历史1900-2015年的CMIP6数据的平均值,上下为四分位。这种图需要historical的数据为(x,y)这里的x为时间,y为时间对应气象要素值。中间的一般为平均值或者中间值,上下表现为四分位范围(但这张图表现为17%-83%),由于最近经常使用这种图,所以结合网上的资料自己修改写了一个子函数可以在python中直接使用

子函数

def tsplot(ax, x, y, n=20, percentile_min=1, percentile_max=99, color='r', plot_mean=True, plot_median=False, line_color='k',label="",**kwargs):

# calculate the lower and upper percentile groups, skipping 50 percentile

perc1 = np.percentile(y, np.linspace(percentile_min, 50, num=n, endpoint=False), axis=0)

perc2 = np.percentile(y, np.linspace(50, percentile_max, num=n+1)[1:], axis=0)

if 'alpha' in kwargs:

alpha = kwargs.pop('alpha')

else:

alpha = 1/n

# fill lower and upper percentile groups

for p1, p2 in zip(perc1, perc2):

ax.fill_between(x, p1, p2, alpha=alpha, color=color, edgecolor=None)

if plot_mean:

ax.plot(x, np.mean(y, axis=0), color=line_color,linewidth='3',label=label)

if plot_median:

ax.plot(x, np.median(y, axis=0), color=line_color,linewidth='3',label=label)

ax.tick_params(labelsize=26)

return ax

函数很好理解,ax为figure添加的图,x和y为上面提到的数据,n为分层的层数(这个可以大家自行体会,我一般不分),percentile_min和max为对应的值(如果使用四分位设置为25和75即可),后面一目了然不再赘述。

这是使用该函数绘制的图

例子

7df5f36c3bf9

由于涉及到未发表的成果所以隐去legend

结束

Enjoy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值