Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnuplot: Shortest way to ignore first line in datafile

Tags:

plot

gnuplot

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?

like image 438
Markus Weninger Avatar asked Feb 20 '16 17:02

Markus Weninger


2 Answers

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.

like image 111
Matthew Avatar answered Sep 23 '22 05:09

Matthew


Perhaps this only applies to current versions and did not exist at the time of the original question, but at present this is now an answer to the original question. Since this page appears at the top when I search for 'how to ignore a line in gnuplot' I thought this update should be mentioned. It seems to exist only in version >= 5.0

There is the Skip keyword.

gnuplot> plot "datapoints.csv" using 1:5 skip 30

" The skip keyword tells the program to skip lines at the start of a text (i.e. not binary) data file. The lines that are skipped do not count toward the line count used in processing the every keyword. Note that skip N skips lines only at the start of the file, whereas every ::N skips lines at the start of every block of data in the file. See also binary skip for a similar option that applies to binary data files."

like image 38
robert atwood Avatar answered Sep 22 '22 05:09

robert atwood