赞
踩
今天继续复现一下朋友发我了一张论文里的图表(原图如下所示),主要是Matplotlib的箱线图+散点图+折线图的组合图,能够非常直观地展现数据的分布情况及其趋势。
由于论文中没提供数据,就拿本地数据做代替,如下图所示,借助pandas读取并转置数据,将年份作为列标签。
import pandas as pd
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)
图表中主要用到了均值和标准差数据,这边可以借助pandas的describe直接获取。
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()
由于两个子图绘制内容一致,将绘图部分代码封装成函数,方便调用,然后调整一下细节就可以了。
# -*- 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 lib
import pandas as pd
import matplotlib.pyplot as plt
import 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()
以上就是本期推文的全部内容了,如果对你有帮助的话,请‘点赞’、‘收藏’,‘关注’,你们的支持是我更新的动力。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。