如何 sprintf() Gnuplot 键中的最后一个数据值?

How to sprintf() last data value in Gnuplot key?

我正在制作温度图,想将最后一个数据点放在标题中。我可以使用 column(2) 来执行此操作,但我还想添加一些描述性文本。我正在尝试使用下面的代码将一些文本与数据值连接起来,但出现此错误:line 0: f_sprintf: attempt to print numeric value with string format

plot "/tmp/data.txt" using 1:2 with lines ls 2 title sprintf('Current :%sF', column(2))

我已经尝试将 sprintf 修饰符更改为 %d 以及与点字符的各种连接方式,但没有找到了正确的组合。

这里的问题是 title 字符串由 gnuplot 分析数据和执行绘图之前评估。

一个技巧是存储温度的最后一个值,然后绘制它。

T=0
plot "/tmp/data.txt" using 1:(T=column(2)) w l ls 2 notitle, \
     1/0 w l ls 2 title sprintf('Current: %.1fF', T)

很可能有多种解决方案。我想到的第一种可能性(我想需要 gnuplot >5.2)是使用 keyentry,检查 help keyentry。在绘图时,您正在将第 2 列分配给一个变量。绘图后,此变量保存第 2 列的最后一个值,您稍后将在 keyentry 中使用它,这是一个不绘制任何内容的键输入。对于旧的 gnuplot 版本也有解决方法。

代码:

### last value into key
reset session

$Data <<EOD
1 7.1
2 6.2
3 5.3
4 4.4
5 3.5
6 2.6
7 1.7
8 0.8
EOD

plot $Data u 1:(a=) w lp pt 7 lc 1 notitle, \
     keyentry w lp pt 7 lc 1 ti sprintf("Last y value: %g",a)

### end of code

结果: