39

I have a file that contains 4 numbers (min, max, mean, standard derivation) and I would like to plot it with gnuplot.

Sample:

24 31 29.0909 2.57451
12 31 27.2727 5.24129
14 31 26.1818 5.04197
22 31 27.7273 3.13603
22 31 28.1818 2.88627

If I have 4 files with one column, then I can do:

gnuplot "file1.txt" with lines, "file2.txt" with lines, "file3.txt" with lines, "file4.txt" with lines

And it will plot 4 curves. I do not care about the x-axis, it should just be a constant increment.

How could I please plot? I can't seem to find a way to have 4 curves with 1 file with 4 columns, just having a constantly incrementing x value.

Thank you.

Wolf
  • 9,679
  • 7
  • 62
  • 108
user1777907
  • 1,355
  • 4
  • 12
  • 28
  • One way I thought of doing, is to put an incremental count on the left, and then use using 1:2, using 1:3.... but maybe there is a way without the extra column please? – user1777907 Apr 18 '13 at 02:03

2 Answers2

92

You can plot different columns of the same file like this:

plot 'file' using 0:1 with lines, '' using 0:2 with lines ...

(... means continuation). A couple of notes on this notation: using specifies which column to plot i.e. column 0 and 1 in the first using statement, the 0th column is a pseudo column that translates to the current line number in the data file. Note that if only one argument is used with using (e.g. using n) it corresponds to saying using 0:n (thanks for pointing that out mgilson).

If your Gnuplot version is recent enough, you would be able to plot all 4 columns with a for-loop:

set key outside
plot for [col=1:4] 'file' using 0:col with lines

Result:

for-loop plot

Gnuplot can use column headings for the title if they are in the data file, e.g.:

min max mean std
24 31 29.0909 2.57451
12 31 27.2727 5.24129
14 31 26.1818 5.04197
22 31 27.7273 3.13603
22 31 28.1818 2.88627

and

set key outside
plot for [col=1:4] 'file' using 0:col with lines title columnheader

Results in:

for-loop plot with column headers

Thor
  • 45,082
  • 11
  • 119
  • 130
  • 6
    I believe that the `0` is unnecessary actually ... `plot for [col=1:4] 'foo' using col` -- This is just a FYI. I actually prefer the script with it in there. – mgilson Apr 18 '13 at 13:02
  • Thank you very much! This is perfect, very insightful. Helped me a lot! – user1777907 Apr 18 '13 at 16:31
  • 1
    I think it is rather unintutive. Anybody knows why the gnuplot syntax is so unexpressive? – Ajoy Nov 07 '16 at 12:58
  • @ajoy general data plotting is complex and needs a full language to do it well. But most tools, including gnuplot, want to just make plots not a language (and end up with a half-baked language anyways) – novelistparty Mar 18 '19 at 18:44
14

Just to add that you can specify the increment in the for loop as third argument. It is useful if you want to plot every nth column.

plot for [col=START:END:INC] 'file' using col with lines

In this case it changes nothing but anyway:

plot for [col=1:4:1] 'file' using col with lines
j_s
  • 350
  • 2
  • 7