介紹
在文獻(xiàn)中能經(jīng)辰焦迹看到一個時間序列圖(橫坐標(biāo)為時間,縱坐標(biāo)為變量)會有陰影覆蓋(一般表現(xiàn)為淡一些的顏色)唾戚,這樣的圖上下為25%-75%的范圍柳洋。可以讓人一眼看出數(shù)據(jù)隨時間變化以及數(shù)據(jù)的波動性叹坦,近幾年用的越來越多膳灶,所以也做了一些努力來還原這種圖。
比如
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).
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數(shù)據(jù)的平均值轧钓,上下為四分位序厉。這種圖需要historical的數(shù)據(jù)為(x,y)這里的x為時間,y為時間對應(yīng)氣象要素值毕箍。中間的一般為平均值或者中間值弛房,上下表現(xiàn)為四分位范圍(但這張圖表現(xiàn)為17%-83%),由于最近經(jīng)常使用這種圖而柑,所以結(jié)合網(wǎng)上的資料自己修改寫了一個子函數(shù)可以在python中直接使用
子函數(shù)
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
函數(shù)很好理解文捶,ax為figure添加的圖,x和y為上面提到的數(shù)據(jù)媒咳,n為分層的層數(shù)(這個可以大家自行體會粹排,我一般不分),percentile_min和max為對應(yīng)的值(如果使用四分位設(shè)置為25和75即可)涩澡,后面一目了然不再贅述顽耳。
這是使用該函數(shù)繪制的圖
例子
由于涉及到未發(fā)表的成果所以隱去legend
結(jié)束
Enjoy