1

We can plot the peaks in GNUplot thanks to the answer in this blog (Find local maximum of data files in gnuplot). Here is my script.

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

The problem now is to add the value of count to the legnd. I tried at as title sprintf("Peaks %d", count) as shown as the last line of my script given above but it is returning 0. If i do print count after the plot block, it gives 23. How can we add the value of a variable to the legend in GNUplot? Any help would be appreciated.

1 Answer 1

3

Title strings are evaluated at the beginning of the plotting instance, therefore any variable that changes during plotting will not be updated when printing the title.

For instance, the following data file looks like sin(x) within the [-10:10] domain and contains three local maxima:

enter image description here

Running the peak script yields 0 in the title because count has been evaluated before the counting had ended:

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

The trick here is to skip this title and then plot a dummy function with the same style as before:

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

Sign up to request clarification or add additional context in comments.

2 Comments

You can change the color with lc [number]; each number corresponds to a color. Also lc rgb "blue" etc. should work.
Thanks so much for the help Miguel. You are very much helpful. I am struggling to ignore the false positives and asked another question: stackoverflow.com/questions/40946762/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.