gTcV
gTcV

Reputation: 2504

How to plot several datasets with titles from one file in Gnuplot?

Assuming I have a file that looks like this (note the double newlines):

"p = 0.1"
1 1
3 3
4 1


"p = 0.2"
1 3
2 2
5 2

Is it possible to make Gnuplot plot these two datasets in one plot with the titles given on the first line of each dataset?

Upvotes: 36

Views: 59015

Answers (5)

gnuplot 5.1 (2016/08/28)

This is similar to https://stackoverflow.com/a/29495496/895245 but with some fixes for later versions.

https://stackoverflow.com/a/43819870/895245 taught me the for [i=0:*] syntax which dispenses stats and is therefore a bit nicer.

Script:

datafile = 'test.dat'
stats datafile nooutput
plot for [IDX=0:STATS_blocks-1] \
    datafile \
    index IDX \
    using 1:2 \
    with lines \
    title columnheader(1)

Test data:

a
1, 1
2, 2
3, 3


"b"
1, 1
2, 4
3, 9


"c, c"
1, 1
2, 8
3, 27

Output:

This works on gnuplot 2016/08/28 which will later become gnuplot 5.1, but not in gnuplot 5.0.3 (Ubuntu 16.04), because in 5.0.3 the stats command gives an error because the column headers are not valid data. And on 2016/08/28 it became just a warning.

I've asked how to remove the warning at: https://groups.google.com/forum/#!topic/comp.graphics.apps.gnuplot/Pi4aBE2PwZ8

Using comments like:

#a
1, 1
2, 2
3, 3

did not work in either version I've tested, it is just ignored.

Upvotes: 4

mkjeldsen
mkjeldsen

Reputation: 2180

This is Bruce_Warrior's and Ciro Santilli's answers but without the intermediate stats:

# plot.gpi
datafile = ARG1
plot for [i=0:*] datafile index i using 1:2\
with lines title columnheader(1)

The for loop can iterate over all datasets in a file directly. It works in gnuplot 5.0.5 but I'm not sure when for acquired this capability. It is documented in the 5.0 manual but not the 4.6 manual.

Unless the line color should be determined by a third input column consumed by linecolor variable (per Bruce's answer), gnuplot will assign different colors and line styles automatically. In this specific case using 1:2 can also be omitted.

$ gnuplot --version
gnuplot 5.0 patchlevel 5
$ gnuplot --persist -c plot.gpi test.dat

Plot de

test.dat is

"p = 0.1"
1 1
3 3
4 1


"p = 0.2"
1 3
2 2
5 2

Upvotes: 8

Bruce_Warrior
Bruce_Warrior

Reputation: 1181

A solution based on answers given by andyras (answer 1 and answer 2) to automatize all the process is to use:

datafile = 'test.dat'
stats datafile
plot for [IDX=1:STATS_blocks] datafile index (IDX-1) u 1:2 w lines t\ 
columnheader(1) lc variable

With this, the script detects automatically the number of data blocks and it plots with different colors and with the corresponding title defined in the first line of each data block.

Upvotes: 4

mgilson
mgilson

Reputation: 309831

It's definitely possible and your datafile is already the correct format. The functionality you're looking for is built into columnheader(N) which reads the data at the top of the N'th column and uses it as the plot title:

 plot 'test.dat' i 0 u 1:2 w lines title columnheader(1),\
      'test.dat' i 1 u 1:2 w lines title columnheader(1)

which can be condensed using iteration:

plot for [IDX=0:1] 'test.dat' i IDX u 1:2 w lines title columnheader(1)

Upvotes: 41

andyras
andyras

Reputation: 15910

With a slight modification of your data set (so that the titles are given as comments):

#"p = 0.1"
1 1
3 3
4 1


#"p = 0.2"
1 3
2 2
5 2

You can plot these two data sets as separate lines like this:

plot 'data.dat' i 0 t "p = 0.1", '' i 1 t "p = 0.2"

The index (i for short) option to the plot command tells gnuplot to plot the ith data set. I can't find a way to get gnuplot to get the titles automatically from the header which is why I specified them manually with the title (t for short) option.

Upvotes: 1

Related Questions