A5. CSV and expressions
Goal: read a CSV with a header row, name the columns by their header, and compute new columns while plotting.
Many tools write CSV with a header row, run.csv too:
text
$ head -n 3 run.csv
iteration,continuity,momentum,energy,cd,dcd
1, 1.00482E+00, 4.51026E-01, 1.77828E+00, 5.36869E-01, 4.00000E-02
2, 9.88751E-01, 4.03780E-01, 1.58489E+00, 5.18076E-01, 2.82843E-02Tell foresight the separator, and the header names the columns:
gnuplot
set datafile separator comma
set key autotitle columnheadgnuplot
plot 'run.csv' using "iteration":(column("cd") * 1e4), \
'' using 1:($1 > 60 ? $5 * 1e4 : 1/0) with points pt 6 title 'after the restart'Why it works
set datafile separator comma(ortab, or any characters";,"): every separator ends a cell; an empty cell holds no number, so its point is skipped.unset datafilegoes back to whitespace.- A quoted name selects a column by its header:
using "iteration":..., andcolumn("cd")inside an expression. set key autotitle columnheadtitles each item with the header of its y column; an explicittitlestill wins. Once headers are in use, the first row of each dataset is a header, not a point.- A parenthesized field is an expression evaluated on each row:
$5andcolumn(5)are column 5,$0the point number; gnuplot's operators and functions, its integer division included. 1/0is undefined:($1 > 60 ? $5 * 1e4 : 1/0)keeps the points after the restart and drops the others.
Try this
using "iteration":(log10(column("continuity"))): a computed column by name.stats 'run.csv' using "cd"thenprint STATS_mean: the numbers behind the plot.set table 'cd.md' markdownbefore the plot: the same columns as a Markdown table (F2).
Pitfalls
5/2is 2 in gnuplot (integer division): write5.0/2.- A header name with blanks needs the quotes of the file (
"wall time") and a separator that is not a blank. $5inside double quotes in a shell-eis the shell's variable: quote the command with single quotes.
Reference: Data Files, column headers, expressions. Next: A6. Two axes and a model.