如何使用Gnuplot将多组折线图数据创建为Fenceplot?
用Gnuplot绘制多组数据的栅栏图(Fenceplot)
原始数据
你的三组数据(空行分隔不同组别)如下:
8 12 6 18 3 4 10 17 14 24 8 22 4 15 6 18 17 26 7 18 2 16 4 12
原始问题
你当前使用的命令是:
splot 'test.dat' u 1:2 w lines
这个命令会把所有数据堆叠在Z=0平面,无法区分组别,需要调整为栅栏图形式。
解决方案
栅栏图的核心是给每组数据分配固定的Z轴值,让不同组别在3D空间的不同Z平面上展示。以下是具体实现方式:
基础手动指定版本
直接为每组数据设置独立Z值,用index参数选择对应数据集:
# 配置图的样式与标签 set style data lines set key outside # 图例移至图外避免遮挡 set xlabel "X" set ylabel "Y" set zlabel "组别" set ticslevel 0 # 消除Z轴默认偏移,让各组贴近底部 # 绘制三组数据,分别对应Z=1、Z=2、Z=3 splot 'test.dat' index 0 u 1:2:(1) title "组1", \ 'test.dat' index 1 u 1:2:(2) title "组2", \ 'test.dat' index 2 u 1:2:(3) title "组3"
循环批量版本(适配多组数据)
如果组数较多,用循环自动生成绘制命令:
set style data lines set key outside set xlabel "X" set ylabel "Y" set zlabel "组别" set ticslevel 0 # 为每组分配不同颜色(可选) set for [i=0:2] style line i+1 lc rgb hsv2rgb(i/3., 1, 1) # 动态生成绘制命令 plot_cmd = "" do for [i=0:2] { plot_cmd = plot_cmd . sprintf("'test.dat' index %d u 1:2:(%d) title '组%d', ", i, i+1, i+1) } # 执行绘制,去掉最后多余的逗号和空格 splot plot_cmd[:-2]
关键说明
index n:Gnuplot默认将空行分隔的数据块视为独立数据集,index 0对应第一组,index 1对应第二组,以此类推。u 1:2:(z_value):第三列指定固定Z轴值,让每组数据在对应Z平面形成折线,最终呈现栅栏状3D效果。
内容的提问来源于stack exchange,提问作者mhearse
相关产品推荐
相关产品推荐

