You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gnuplot 5.4.2多文件多列箱线图绘制及优化问题

Gnuplot 5.4.2 多列/多文件箱线图绘制解决方案

问题解答

1. 能否使用包含NaN的合并数据绘制箱线图?

完全可以。Gnuplot在处理箱线图时会自动忽略NaN值,只要每列的有效数据量足够支撑分位数计算,就不会影响绘图结果。例如合并后的combined.dat可以是这样的格式:

1.2 3.1 NaN
2.5 NaN 4.3
NaN 2.8 5.1

直接用boxplot命令读取即可,Gnuplot会跳过NaN,仅基于有效数据计算统计量。

2. 是否可将首行设为箱线图标签?

可以。通过columnhead()函数调用首行文本作为标签。假设合并数据文件combined.dat的首行为各分组名称:

GroupA GroupB GroupC
1.2 3.1 NaN
2.5 NaN 4.3
...

对应的绘图命令如下:

set style data boxplot
plot 'combined.dat' using 1:xtic(0) title columnhead(1), \
     '' using 2:xtic(0) title columnhead(2), \
     '' using 3:xtic(0) title columnhead(3)

columnhead(n)会读取第n列的首行内容,作为对应箱线图的标题。

3. 如何在箱线图中额外绘制均值?

箱线图默认展示中位数、分位数和异常值,要叠加均值可以通过stats命令提前计算均值,再用点样式叠加绘制。示例脚本如下:

# 提前统计各列均值,不输出统计信息
stats 'combined.dat' nooutput
# 设置箱线图样式
set style boxplot outliers pointtype 7
set style fill solid 0.25 border -1
# 绘制箱线图+均值点
plot 'combined.dat' using 1:xtic(1) title columnhead(1), \
     '' using 2:xtic(2) title columnhead(2), \
     # 第一列均值,x轴位置对应1,用6号菱形点标记
     '' using (1):(STATS_mean_1) with points pointtype 6 title '均值', \
     # 第二列均值,不重复显示标题
     '' using (2):(STATS_mean_2) with points pointtype 6 notitle

STATS_mean_x是stats命令计算出的第x列均值,通过指定x轴位置(如(1))将均值点对应到对应箱线图的上方。

多文件数据直接绘制方法

无需合并文件,直接在plot命令中依次读取多个文件,同时手动指定统一的y轴范围即可:

# 设置统一的y轴范围,确保所有箱线图对齐
set yrange [0:10]
set style data boxplot
# 依次绘制多个单文件的箱线图
plot 'file1.dat' using 1:xtic('File1') title 'File1', \
     'file2.dat' using 1:xtic('File2') title 'File2', \
     'file3.dat' using 1:xtic('File3') title 'File3'

如果单个文件包含多列数据,也可以分别指定列绘制:

plot 'file1.dat' using 1:xtic('File1-Col1') title 'File1-Col1', \
     'file1.dat' using 2:xtic('File1-Col2') title 'File1-Col2', \
     'file2.dat' using 1:xtic('File2-Col1') title 'File2-Col1'

内容的提问来源于stack exchange,提问作者emacs drives me nuts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 18:05:11