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

如何使用GNUPlot绘制与首个测量值的差值数据线?

GNUPlot 绘制测量点与对应首个值的差值图表

问题背景

我正在使用GNUPlot绘制约18500个测量点的图表,数据源格式如下:

Date/time           | Name   | Value
21.07.2022 17:00:16 | M.01 - | 4045
21.07.2022 17:00:25 | M.02 - | 1789
21.07.2022 17:00:35 | M.03 - | 3245
21.07.2022 17:04:15 | M.01 - | 4043
21.07.2022 17:05:01 | M.02 - | 1793
21.07.2022 17:05:14 | M.03 - | 3246
21.07.2022 17:09:45 | M.01 - | 4042
21.07.2022 17:10:05 | M.02 - | 1793
21.07.2022 17:10:29 | M.03 - | 3247

当前使用的脚本可正常绘制原始值,但需要修改为显示各测量点与对应首个值的差值(例如M.01后续值与4045的差值为-2、-3等):

set datafile separator ';'
set xdata time
set xlabel "Zeit"
set ylabel "Abweichung"
set timefmt "%d-%m-%Y %H:%M:%S" 
plot 'D:\_Local\gnuplot\source.csv' using 1:(stringcolumn(2) eq "M.01 -"? $3:1/0) title "M.01" lc rgb "blue" , "" using 1:(stringcolumn(2) eq "M.02 -"? $3:1/0) title "M.02" lc rgb "red", "" using 1:(stringcolumn(2) eq "M.03 -"? $3:1/0) title "M.03" lc rgb "green"

解决方案

要实现这个需求,可以在GNUPlot中初始化变量存储每个测量项的首个值,第一次读取到对应项时记录初始值,后续计算当前值与初始值的差值。

修改后的脚本如下:

set datafile separator ';'
set xdata time
set xlabel "Zeit"
set ylabel "Abweichung"
set timefmt "%d-%m-%Y %H:%M:%S" 

# 初始化存储首个值的变量,初始为NaN(标记未读取过初始值)
m01_first = NaN
m02_first = NaN
m03_first = NaN

# 自定义函数:检查名称匹配,首次匹配时记录初始值,返回当前值与初始值的差值
get_diff(col, target_name, init_var) = (stringcolumn(col) eq target_name ? (init_var = isnan(init_var) ? $3 : init_var, $3 - init_var) : 1/0)

plot 'D:\_Local\gnuplot\source.csv' \
    using 1:(get_diff(2, "M.01 -", m01_first)) title "M.01" lc rgb "blue", \
    "" using 1:(get_diff(2, "M.02 -", m02_first)) title "M.02" lc rgb "red", \
    "" using 1:(get_diff(2, "M.03 -", m03_first)) title "M.03" lc rgb "green"

关键说明

  • 初始化m01_first、m02_first、m03_first为NaN,用来判断是否已读取到对应测量项的首个值。
  • 函数get_diff的逻辑:
    • 先检查当前行的名称是否匹配目标名称
    • 如果是首次匹配(变量为NaN),就将当前值赋值给变量
    • 返回当前值与初始值的差值;不匹配则返回1/0(跳过该数据点)
  • 绘图时调用该函数,即可自动计算并绘制差值曲线

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 20:57:26