34

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?

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
  • Is `every ::1` really so bad? It seems elegant to me. Where do the title strings `"B"`, `"C"`, etc., come from? Are they column headers? – e0k Feb 20 '16 at 18:15
  • 1
    Not "so bad", but I would find it more elegant if this could be done in one statement instead of in every plot. If I imagine of a bar plot with 5 different bars, and additional error bars, this would result in 10 plot statements, and therefore 10 `every ::1`. And yes, `"A"`, `"B"`, etc. are just example column headers that should be shown in the key / legend. – Markus Weninger Feb 20 '16 at 18:18
  • 2
    Do you know about plot iteration? Type `help plot iteration` from a `gnuplot` prompt. – e0k Feb 20 '16 at 18:59
  • I'm currently on my way going out, but I will look into this when coming home, thank you! – Markus Weninger Feb 20 '16 at 19:14
  • Related: https://stackoverflow.com/questions/773643/gnuplot-labels – Ciro Santilli OurBigBook.com Apr 27 '19 at 16:44

3 Answers3

44

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.

Matthew
  • 7,440
  • 1
  • 24
  • 49
  • 2
    Be sure to turn on macros with `set macros` before you use any `@`. – e0k Feb 20 '16 at 22:00
  • If the macros don't work, try the command suggested by @e0k and then try them again. However, I haven't yet encountered an installation of gnuplot that I have had to issue that command. – Matthew Feb 20 '16 at 22:31
  • 1
    Thank you very much! This seems to be exactly what I wanted, and I also wandered if something like this `@` was possible. Two answers in one :) – Markus Weninger Feb 21 '16 at 09:37
7

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."

robert atwood
  • 71
  • 1
  • 1
  • RIght, `skip` is available at least since gnuplot 4.6.7 (April, 2015). So, it's not clear which version the OP and the answer are talking about. – theozh Aug 12 '22 at 14:05
0

To ignore first line :

file="file.txt"
plot sprintf("< tail -n +2 %s", file) u 1:2 w lp
#plot "< tail -n +2 ".file u 1:2 w lp #<== either

But in your context, using the first line as a header is better

set key autotitle columnhead
douardo
  • 697
  • 6
  • 6