GNUplot:在图例中使用变量

GNUplot: using variables in the legend

多亏了这个博客中的答案,我们可以在 GNUplot 中绘制峰值 ()。这是我的脚本。

xcolumn=1
ycolumn=0
count = 0


plot "test.csv" u (column(xcolumn)):(column(ycolumn)) title "freq" w l, \
     "test.csv" u (column(0)==0 ? (last2y=column(ycolumn), \
     last2x=column(xcolumn), 1/0) : column(0)==1 ? (lasty=column(ycolumn), \
     lastx=column(xcolumn), 1/0) : lastx) \
     : \
     ( column(0) < 2 ? 1/0 : (last2y <= lasty && \
     column(ycolumn) < lasty) ? (value=lasty, last2y=lasty, last2x=lastx, \
     count = count +1, \
     lasty=column(ycolumn), lastx=column(xcolumn), value) : (last2y=lasty, \
     last2x=lastx, lasty=column(ycolumn), lastx=column(xcolumn), 1/0))  title sprintf("Peaks %d", count) pt 7

现在的问题是将count的值添加到图例中。我尝试了 as title sprintf("Peaks %d", count),如上面给出的脚本的最后一行所示,但它返回 0。如果我在绘图块之后执行 print count,它给出 23。我们如何添加值GNUplot 中图例的变量?任何帮助将不胜感激。

标题字符串在绘图实例开始时计算,因此在打印标题时不会更新在绘图过程中更改的任何变量。

例如,以下数据文件看起来像 [-10:10] 域内的 sin(x) 并且包含三个局部最大值:

运行 峰值脚本在标题中产生 0,因为 count 在计数结束之前已被评估:

xcolumn=1
ycolumn=2
count=0

plot "sin.dat" u (column(xcolumn)):(column(ycolumn)) w l, \
"sin.dat" u (column(0)==0 ? (last2y=column(ycolumn), \
last2x=column(xcolumn), 1/0) : column(0)==1 ? (lasty=column(ycolumn), \
lastx=column(xcolumn), 1/0) : lastx) \
: \
( column(0) < 2 ? 1/0 : (last2y < lasty && \
column(ycolumn) < lasty) ? (count=count+1, value=lasty, last2y=lasty, last2x=lastx, \
lasty=column(ycolumn), lastx=column(xcolumn), value) : (last2y=lasty, \
last2x=lastx, lasty=column(ycolumn), lastx=column(xcolumn), 1/0)) pt 7 t sprintf("No. of peaks = %i", count)

这里的技巧是跳过这个标题,然后绘制一个与之前相同样式的虚拟函数:

xcolumn=1
ycolumn=2
count=0

plot "sin.dat" u (column(xcolumn)):(column(ycolumn)) w l, \
"sin.dat" u (column(0)==0 ? (last2y=column(ycolumn), \
last2x=column(xcolumn), 1/0) : column(0)==1 ? (lasty=column(ycolumn), \
lastx=column(xcolumn), 1/0) : lastx) \
: \
( column(0) < 2 ? 1/0 : (last2y < lasty && \
column(ycolumn) < lasty) ? (count=count+1, value=lasty, last2y=lasty, last2x=lastx, \
lasty=column(ycolumn), lastx=column(xcolumn), value) : (last2y=lasty, \
last2x=lastx, lasty=column(ycolumn), lastx=column(xcolumn), 1/0)) pt 7 not ,\
1/0 w p lc 2 pt 7 t sprintf("No. of peaks = %i", count)