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

如何绘制文件中分离数据块对应行的连线散点图?

多数据块对应行连线图绘制方案(Gnuplot实现)

数据文件包含TP1、TP2……TPn等多个数据块,如何将每个数据块的第1行、第2行、第3行……对应位置的点用linespoint样式绘制为连线图?

示例

第一条绘制线:

TP1 -> (37.74, 7030.26)
TP2 -> (26.42, 7652.64)
...

第二条绘制线:

TP1 -> (37.74, 7030.26)
TP2 -> (33.14, 7705.56)
...

实现代码

reset 

GROUP_LABELS    = "1 2 3"
n_group_labels  = words(GROUP_LABELS)
in_group        = '( $1 == int(word(GROUP_LABELS, i)) ? 1.0 : NaN )'

$Data << EOD
# TP1
gp   HS   Age     vol_01    vol_02
2    0    37.74   7030.26   7353.44
2    0    26.42   7652.64   8566.62
2    0    30.20   6938.74   7489.41
2    0    27.68   6619.97   6224.74
2    1    18.87   7472.23   8635.48
2    0    34.56   6612.52   7347.70
2    0    33.54   7049.64   7137.09
3    1    21.47   8327.11   8745.67
1    0    22.00   7924.66   8694.33
1    0    24.96   7605.58   8072.01
3    1    12.10   8883.32   9230.87
2    0    24.72   8271.73   8465.82


# TP 2
gp   HS   Age     vol_01    vol_02
2    0    38.84   7069.54   7380.43
2    0    33.14   7705.56   8076.87
2    0    39.78   7346.33   7712.80
2    0    28.98   6963.54   7171.83
2    1    23.71   6830.13   8113.97
2    0    39.93   7438.96   7020.43
2    0    44.92   6943.02   7204.57
3    1    26.82   8245.33   8335.07
1    0    24.93   7396.14   7853.01
1    0    29.79   7421.25   7810.83
3    1    14.55   8672.95   8937.76
2    0    27.30   8668.11   8506.81



EOD

plot 0 lw 1 lc -1 notitle, \
    for [i=1:n_group_labels] $Data using ( @in_group * column(3)):(column(4)) w lp pt 7 ps 2 lc i 

代码说明

  • 分组配置:GROUP_LABELS定义需要绘制的分组编号,n_group_labels通过words()函数自动获取分组总数
  • 数据筛选:in_group表达式判断每行数据的分组是否匹配当前循环的分组,不匹配的点设为NaN,gnuplot会自动忽略这类点
  • 循环绘制:通过for循环遍历每个分组,使用linespoint(缩写w lp)样式绘制连线图,同时指定点形状(pt 7)、点大小(ps 2)和颜色(lc i)
  • 多数据块处理:gnuplot会将以#开头的行识别为数据块分隔符,绘制时会自动提取每个数据块中对应行的匹配点并连接成线

内容的提问来源于stack exchange,提问作者magfan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 07:17:06