【跟着SCI学作图】Matplotlib fill_between的简单应用

文摘   教育   2023-01-22 12:05   江苏  


01 引言:

今天接着复现【Future increases in Arctic lightning and fire risk for permafrost carbon】的图表,主要是Matplotlib的fill_between和fill_betweenx的简单应用。

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

数据下载地址:

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

02 代码如下:

# -*- encoding: utf-8 -*-# -*- encoding: utf-8 -*-'''@File    :   png3.py@Time    :   2022/12/28 13:19:42@Author  :   HMX @Version :   1.0@Contact :   kzdhb8023@163.com'''# here put the import libimport osimport pandas as pdimport matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np

os.chdir(r'E:\CODE\work\plot7\png3\data')data = '41558_2021_1011_MOESM4_ESM.csv'
df = pd.read_csv(data,index_col=0)df.dis = df.dis*-1print(df)fig,ax = plt.subplots(figsize = (6,3),dpi = 100)lns1 = ax.plot(df.dis,df.FR_HIST,color = 'r',label = 'Present day (1986–2005)')ax.fill_between(df.dis,df.FR_HIST-df.FRstd_HIST,df.FR_HIST+df.FRstd_HIST,alpha = 0.2,color = 'r')lns2 = ax.plot(df.dis,df.FR_FT,label = 'Future (2081–2100)',linestyle = ':')ax.fill_between(df.dis,df.FR_FT-df.FRstd_FT,df.FR_FT+df.FRstd_FT,alpha = 0.2)ax.set_ylim(0,1)ax.set_xlim(-800,500)ax2 = ax.twinx()lns3 = ax2.plot(df.dis,df.dFR,color = 'purple',label ='Change')ax2.set_ylim(0,250)ax2.set_yticks(np.arange(0,241,80))ax2.fill_betweenx([0,250],[0,0],[500,500],color = 'tab:orange',alpha = 0.2,zorder = 0)lns = lns1+lns2+lns3labs = [l.get_label() for l in lns]ax.legend(lns, labs,loc = 'upper left',frameon=False)ax.set_ylabel('Lightning flash rate'+'\n'+'(number per km$^{2}$ per month)')ax2.set_ylabel('Flash rate change (%)',color = 'purple',rotation = 270,labelpad = 20)ax.set_xlabel('Distance north of treeline (km)')plt.tight_layout()plt.savefig('png3.png',dpi = 600)plt.show()

03 结果如下:

欢迎私交流学习


戳这里关注我

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

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