Search code examples
plotgnuplot

Gnuplot: Shortest way to ignore first line in datafile


I have a .csv datafile created by another 3rd party application that should be plotted using gnuplot. Let's assume the file has the following format:

1;2;3;4;5;6 <-- This is the header line that should be ignored (with values 1;2;...;N)
1;1;2;1;1;1
2;3;3;3;5;6
3;4;1;1;1;4

The first column is the x-axis, the following columns should each be plotted as own lineplot (yes, I know, too many line plots within one plot may look bad, but just to get an idea). Here a MCVE:

set terminal png size 1000,500    
set datafile separator ";" # CSV file is seperated with ;
plot \
  'C://tmp/test.csv' using 1:2 with lines title "A",\
  'C://tmp/test.csv' using 1:3 with lines title "B",\
  'C://tmp/test.csv' using 1:4 with lines title "C",\
  'C://tmp/test.csv' using 1:5 with lines title "D",\
  'C://tmp/test.csv' using 1:6 with lines title "E"

The problem is, that this also plots the first line as it would be data.

I know that to can ignore any line in the datafile by starting it with #, like #1;2;3;4;5;6, yet I do not want to edit the file, because it is also used by other tools.

Another way is to use plot <filename> every ::1 to ignore the first line, which would mean that I would have to include every ::1 5 times in the above script, as explained in this link. This would look like the following:

set terminal png size 1000,500    
set datafile separator ";" # CSV file is seperated with ;
plot \
  'C://tmp/test.csv' every ::1 using 1:2 with lines title "A",\
  'C://tmp/test.csv' every ::1 using 1:3 with lines title "B",\
  'C://tmp/test.csv' every ::1 using 1:4 with lines title "C",\
  'C://tmp/test.csv' every ::1 using 1:5 with lines title "D",\
  'C://tmp/test.csv' every ::1 using 1:6 with lines title "E"

Is defining every ::1 for every plot really the only way? Is there some shorter - preferable one-liner - way to ignore the first (n) line(s); some way to define the every ::1 "globally" or something like (pseudocode) set datafile ignorefirstnlines 1?


Solution

  • Gnuplot can read column names from the first line, but you can still specify the column names normally as well. Therefore, this effectively skips the first line.

    Issue the command

    set key autotitle columnhead
    

    This tells gnuplot that the first line is not data, but is the column names to be used for the key. You can still unset key or plot datafile title sometitle the same as you could before, and gnuplot will just not use that data.

    Suppose that my file looks like

    1 2
    4 5
    7 8
    

    I can just issue set key autotitle columnhead follwed by unset key (if I don't really want a key), and it will skip the first line.

    enter image description here


    Alternatively, I can pipe my data through an external program. For example, using awk (which is available for most OS's including Windows), I can do

    plot "< awk '(NR>2){print;}' datafile"
    

    to skip the first 2 lines (using Windows, I must do '< awk "(NR>2){print;}" datafile'). If I don't want to keep typing this, I can store this in a string

    skipfile = "\"< awk '(NR>2){print;}' datafile\""
    

    and use it as a macro (for Windows, use skipfile = '"< awk \"(NR>2){print;}\" datafile"'). For instance, to plot the datafile using lines, I might do

    plot @skipfile with lines
    

    The @skipfile just tells gnuplot to treat the command like I had just typed the contents of skipfile right there.