2016-11-30 7 views
1

Wir können die Spitzen in GNUplot dank der Antwort in diesem Blog (Find local maximum of data files in gnuplot) plotten. Hier ist mein Skript.GNUplot: Verwenden von Variablen in der Legende

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 

Das Problem ist nun, den Wert von count zum legnd hinzuzufügen. Ich habe versucht, als title sprintf("Peaks %d", count) wie gezeigt als die letzte Zeile meines Skripts oben gegeben, aber es gibt 0 zurück. Wenn ich print count nach dem Plot-Block, gibt es 23. Wie können wir den Wert einer Variablen der Legende in GNUplot hinzufügen? Jede Hilfe wäre willkommen.

Antwort

2

Titelzeichenfolgen werden am Anfang der Zeicheninstanz ausgewertet, daher wird jede Variable, die sich während des Zeichnens ändert, beim Drucken des Titels nicht aktualisiert.

Zum Beispiel die folgende Datendatei sieht aus wie sin (x) in der [-10: 10] Domäne und enthält drei lokale Maxima:

enter image description here

Ausführen des Spitzen Skript liefert 0 im Titel

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

: weil count ausgewertet wurde, bevor hatte die Zählung beendet

Der Trick hier ist, diesen Titel zu überspringen und dann eine Dummy-Funktion mit dem gleichen Stil wie plotten vor:

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

+1

Sie die Farbe mit 'lc [Nummer] ändern kann'; jede Zahl entspricht einer Farbe. Auch 'lc rgb" blau "usw. sollte funktionieren. – Miguel

+0

danke und es funktioniert! – Mahsolid

+0

Vielen Dank für die Hilfe Miguel. Sie sind sehr hilfreich. Ich bemühe mich, die falsch positiven zu ignorieren und stellte eine andere Frage: http://stackoverflow.com/questions/40946762/how-to-control-false-positives-in-gnuplot – Mahsolid

Verwandte Themen