How to plot several datasets with titles from one file in Gnuplot?
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)
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
test.dat
is
"p = 0.1"
1 1
3 3
4 1
"p = 0.2"
1 3
2 2
5 2