【跟着SCI学作图】Matplotlib&Seaborn绘制散点密度图&直方图

文摘   教育   2023-01-19 21:30   江苏  


01 引言:

最近读文献【Future increases in Arctic lightning and fire risk for permafrost carbon】发现文中图表的排版布局非常好,故借鉴一下。主要用的就是一个散点密度图+两个直方图。

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

数据下载地址:

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

02 代码如下:

# -*- encoding: utf-8 -*-'''@File    :   png1.py@Time    :   2022/12/27 20:28:31@Author  :   HMX @Version :   1.0@Contact :   kzdhb8023@163.com'''# here put the import libimport pandas as pdimport osimport matplotlib.pyplot as pltfrom scipy.stats import gaussian_kdefrom mpl_toolkits.axes_grid1 import make_axes_locatableimport numpy as npfrom matplotlib import ticker, cmimport seaborn as sns

os.chdir(r'E:\CODE\work\plot7\png1\data')# part a# scatterdf = pd.read_csv('Fig1a_XY_obs.csv',index_col=0)x,y = df.CxP.values,df.FR.valuesxy = np.vstack([x,y])z = gaussian_kde(xy)(xy)idx = z.argsort()x, y, z = x[idx], y[idx], z[idx]fig, ax = plt.subplots(figsize=(6,6),dpi=100)xcord=xycord=ym = ((xcord*ycord).mean() - xcord.mean()* ycord.mean())/(pow(xcord,2).mean()-pow(xcord.mean(),2))c = ycord.mean() - m*xcord.mean()scatter=ax.scatter(x,y,c=z,s = 2)divider=make_axes_locatable(ax)ax.set_xlim(0,0.01)ax.set_ylim(0,1.5)ax.set_yticks(np.arange(0,1.51,0.5))ax_histx = divider.append_axes("bottom", 1.2, pad=0.15)ax_histy = divider.append_axes("right", 1.2, pad=0.15)ax.set_xticklabels([])# line ds = pd.read_csv('Fig1a_XY_mod.csv',index_col=0)print(ds)print(ds.columns)ys = ['Y_pl', 'Y_pl_op', 'Y_sc', 'Y_li1', 'Y_np', 'Y_mean']linestyles = ['-.','-.','-.','-.','-.','-']colors = ['#723BA0','#8575D7','#8DCAD0','#E0C671','#9F795D','k']labels = ['Power law','Power law (linear opt.)','Scale','Linear','Non-parametric','Mean']for i in range(len(ys)): y = ds[ys[i]].values ax.plot(ds.X,y,linestyle=linestyles[i],c = colors[i],label = labels[i]) ax.legend(frameon=False)ax.set_ylabel('Lightning flash rate (number per km$^{2}$ per month)')ax.set_xticks([0,0.005,0.010])
# part bdf2 = pd.read_csv('Fig1b_CxP.csv',index_col=0)sns.histplot(df2['rcp'].values, kde=True,ax = ax_histx,edgecolor = 'w',bins=51)sns.histplot(df2['hist'].values, kde=True,ax = ax_histx,edgecolor = 'w',bins=30,color = 'red')
ax_histx.plot(1,1,label = 'Present day',c = '#D4A663')ax_histx.plot(1,1,label = 'Future',c = '#0202FF')ax_histx.legend(frameon=False)ax_histx.set_ylabel('Norm. dist.')ax_histx.set_xlabel('CAPE × Precip (W m$^{2}$)')
ax_histx.set_xticks([0,0.005,0.010])ax_histx.set_xticklabels([0,0.005,'0.010'])ax_histx.set_xlim(0,0.01)ax_histx.set_ylim(0,400)ax_histx.set_yticks([0,200,400])# part cdf3 = pd.read_csv('Fig1c_FR.csv',index_col=0)ax_histy.set_xlabel('Norm. dist.')ax_histy.set_yticks([])ax_histy = ax_histy.twinx()ax_histy.set_ylabel('Lightning flash rate (number per km$^{2}$ per month)',rotation = 270,labelpad = 15)sns.histplot(y = df3['rcp'].values, kde=True,ax = ax_histy,edgecolor = 'w',bins=30)sns.histplot(y = df3['hist'].values, kde=True,ax = ax_histy,color = 'red',edgecolor = 'w',bins=30)ax_histy.set_xlim(350,0)ax_histy.set_xticklabels([0,2.5])ax_histy.set_ylim(0,1.5)ax_histy.set_yticks(np.arange(0,1.51,0.5))ax_histy.set_xlabel('Norm. dist.')plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = Trueplt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = Falseplt.savefig('png1.png',dpi = 600)plt.show()

03 结果如下:

欢迎私交流学习


戳这里关注我

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

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