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

如何在gnuplot中连接单个数据文件内不同索引的数据点

Gnuplot连接两个数据块的对应索引点

方法1:数组存储坐标 + 循环绘箭头(推荐,Gnuplot 5.0+)

利用Gnuplot的数组功能先缓存两个数据块的坐标,再循环生成箭头:

# 定义数组存储两组点的坐标
array x1[3]
array y1[3]
array x2[3]
array y2[3]

# 读取第一个数据块的坐标到数组
plot "a_test.dat" index "first" u (x1[$0+1]=$1, y1[$0+1]=$2) notitle
# 读取第二个数据块的坐标到数组
plot "a_test.dat" index "second" u (x2[$0+1]=$1, y2[$0+1]=$2) notitle

# 循环创建箭头,连接对应索引的点
do for [i=1:3] {
    set arrow i from x1[i], y1[i] to x2[i], y2[i] nohead lw 2 lc "red"
}

# 绘制原始数据点
plot "a_test.dat" index "first" u 1:2 pt 7 lc "blue" title "first block", \
     "" index "second" u 1:2 pt 7 lc "green" title "second block"

方法2:外部命令读取行数据(兼容低版本Gnuplot)

通过sed读取指定行的坐标,手动生成箭头:

# 获取第一个数据块的行数
stats "a_test.dat" index "first" nooutput
point_count = STATS_records

# 循环读取对应行并创建箭头
do for [i=0:point_count-1] {
    # 读取first块第i+3行(跳过注释和表头)
    x1 = word(system("sed -n '".(i+3)."p' a_test.dat"), 1)
    y1 = word(system("sed -n '".(i+3)."p' a_test.dat"), 2)
    # 读取second块第i+8行(跳过前面的注释、表头和空行)
    x2 = word(system("sed -n '".(i+8)."p' a_test.dat"), 1)
    y2 = word(system("sed -n '".(i+8)."p' a_test.dat"), 2)
    set arrow i+1 from x1,y1 to x2,y2 nohead lw 2 lc "red"
}

# 绘制数据点
plot "a_test.dat" index "first" u 1:2 pt 7 lc "blue" title "first", \
     "" index "second" u 1:2 pt 7 lc "green" title "second"

关键参数说明

  • nohead:隐藏箭头头部,改为head可显示指向第二个数据块的箭头
  • lw:设置箭头线宽,lc:设置箭头颜色
  • $0:Gnuplot内置变量,代表当前数据点的索引(从0开始)
  • STATS_records:stats命令返回的当前数据块的有效行数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 04:18:22