The simplest method to plot multiple data files is to insert a for
loop inside the plot
command of
gnuplot. Assuming you have N
files named sequently, i.e.
file_1.dat
file_2.dat
file_3.dat
...
file_N.dat
Executing the command
plot for[i = 1:N] "file_".i.".dat"
will plot all the files between file_1.dat
and file_N.dat
in the same graph.
Example with three data files
Table of datasets
X-Axes | Y-Axe file_1.dat | Y-Axe file_2.dat | Y-Axe file_3.dat |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 | 4 | 2 |
3 | 3 | 9 | 6 |
4 | 4 | 16 | 24 |
5 | 5 | 25 | 120 |
Commands
set terminal postscript color noenhanced ##setting the term
set output "multiple_files.ps"
set key center ##legend placement
plot [1:5][1:120] \
for [i = 1:3] "file_".i.".dat" \
pointsize 1.3 linecolor i+4 \
title "file\_".i.".dat" \
with linespoint
The loop starts with for [i = 1:3] "file_".i.".dat"
and execute the plot
command until it reaches i = 3
. The .i.
is the concatenated number.
title "file\_".i.".dat"
has been written with the \
in order to make the _
symbol in the name of the files appears as an underscore
rather than a subscript, and noenhanced
specifier is fundamental to obtain this result.
The final result is shown below
sprintf
functionAnother possible path to follow is using the sprintf
function that works
basically the same as the C-language sprintf
.
The right syntax, from the gnuplot 5.1 documentation is
sprintf("format", x, y, ...)
A brief example will clarify every doubt.
file_name(n) = sprintf("file_%d.dat", n)
plot for[i = 1:N] file_name(i) title file_name(i)