Skip to content

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-02

Tell foresight the separator, and the header names the columns:

gnuplot
set datafile separator comma
set key autotitle columnhead
gnuplot
plot 'run.csv' using "iteration":(column("cd") * 1e4), \
     ''        using 1:($1 > 60 ? $5 * 1e4 : 1/0) with points pt 6 title 'after the restart'
wheel: zoom · drag: pan · right drag: box · a resetopen page ↗

Why it works ​

  • set datafile separator comma (or tab, or any characters ";,"): every separator ends a cell; an empty cell holds no number, so its point is skipped. unset datafile goes back to whitespace.
  • A quoted name selects a column by its header: using "iteration":..., and column("cd") inside an expression.
  • set key autotitle columnhead titles each item with the header of its y column; an explicit title still 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: $5 and column(5) are column 5, $0 the point number; gnuplot's operators and functions, its integer division included.
  • 1/0 is 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" then print STATS_mean: the numbers behind the plot.
  • set table 'cd.md' markdown before the plot: the same columns as a Markdown table (F2).

Pitfalls ​

  • 5/2 is 2 in gnuplot (integer division): write 5.0/2.
  • A header name with blanks needs the quotes of the file ("wall time") and a separator that is not a blank.
  • $5 inside double quotes in a shell -e is the shell's variable: quote the command with single quotes.

Reference: Data Files, column headers, expressions. Next: A6. Two axes and a model.

foresight is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT (License).