【跟着SCI学作图】Matplotlib boxplot绘制箱线图

文摘   教育   2023-04-12 20:00   北京  


01

# 引言         

今天继续复现一下朋友发我了一张论文里的图表(原图如下所示),主要是Matplotlib的箱线图+散点图+折线图的组合图,能够非常直观地展现数据的分布情况及其趋势。

02

# 读取数据         

由于论文中没提供数据,就拿本地数据做代替,如下图所示,借助pandas读取并转置数据,将年份作为列标签。

import pandas as pdfn = r'D:\ForestMeteorology\FM230331\data\1.xlsx'df = pd.read_excel(fn)df1 = df.iloc[:,:11]df1.set_index('w1',inplace=True)df1.columns = ['v'+str(i) for i in range(1,11)]df1 = df1.Tprint(df1)

03

# 可视化         

fig,axs = plt.subplots(1,2,figsize=(8,4))ax = axs[0]# 箱线图df1.boxplot(ax = ax,showfliers = False,grid = False,color = 'black')# 折线图ax.plot(range(1,2020-1995+2),df1.describe().loc['mean'],c = 'k',marker = 's',markersize = 3.5,zorder = 10)# 散点图for j in range(len(df1)):    ax.scatter(range(1,2020-1995+2),df1.iloc[j,:],s = 3.5,zorder = 5)ax.set_xticks(range(1,2020-1995+2,5))ax.set_xticklabels(range(1995,2021,5))plt.show()

04

# 完整代码          

由于两个子图绘制内容一致,将绘图部分代码封装成函数,方便调用,然后调整一下细节就可以了。

# -*- encoding: utf-8 -*-'''@File    :   GZH.py@Time    :   2023/03/31 23:02:23@Author  :   HMX@Version :   1.0@Contact :   kzdhb8023@163.com'''
# here put the import libimport pandas as pdimport matplotlib.pyplot as pltimport matplotlib as mpl
fontdict = {'weight': 'normal','size':10,'color':'k','family':'SimHei'}mpl.rcParams.update( { 'text.usetex': False, 'font.family': 'stixgeneral', 'mathtext.fontset': 'stix', "font.family":'serif', "font.size": 10, "mathtext.fontset":'stix', "font.serif": ['Times New Roman'], } )
def df2png(idf,ax,label): # 箱线图 idf.boxplot(ax = ax,showfliers = False,grid = False,color = 'black') # 折线图 ax.plot(range(1,2020-1995+2),idf.describe().loc['mean'],c = 'k',marker = 's',markersize = 3.5,zorder = 10) # 散点图 for j in range(len(idf)): ax.scatter(range(1,2020-1995+2),idf.iloc[j,:],s = 3.5,zorder = 5)
ax.set_xticks(range(1,2020-1995+2,5)) ax.set_xticklabels(range(1995,2021,5)) ax.set_xlabel('年份',fontdict =fontdict) ax.set_ylabel('森气笔记',fontdict =fontdict) ax.set_ylim(9,15) ax.set_title(label,loc = 'left',y = 0.9)

# 读取处理数据fn = r'D:\ForestMeteorology\FM230331\data\1.xlsx'df = pd.read_excel(fn)df1 = df.iloc[:,:11]df1.set_index('w1',inplace=True)df1.columns = ['v'+str(i) for i in range(1,11)]df1 = df1.T# print(df1)df2 = df.iloc[:,13:-1]df2.set_index('w2',inplace=True)df2.columns = ['v'+str(i) for i in range(1,11)]df2 = df2.T
# 可视化fig,axs = plt.subplots(1,2,figsize=(8,4))ax1 = axs[0] ax2 = axs[1]df2png(df1,ax1,' (a)')df2png(df2,ax2,' (b)')plt.tight_layout()plt.savefig(r'D:\ForestMeteorology\FM230331\data\GZH.png',dpi = 600)plt.show()

欢迎私交流学习


戳这里关注我

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

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