GNUplot:使用图例中的变量

时间:2016-11-30 13:23:58

标签: gnuplot legend legend-properties

由于此博客中的答案(Find local maximum of data files in gnuplot),我们可以在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的值添加到legnd。我尝试了title sprintf("Peaks %d", count),如上面给出的我脚本的最后一行所示,但它返回0.如果我在绘图块之后执行print count,它会给出23.我们怎样才能添加值GNUplot中图例的变量?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

标题字符串在绘图实例的开头进行评估,因此在打印标题时不会更新在绘图期间更改的任何变量。

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

enter image description here

运行高峰脚本会在标题中产生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)

enter image description here

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

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)

enter image description here