Gnuplot Basic plotting of data files Plotting multiple data files


Example

First method - Concatenation of strings

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-AxesY-Axe file_1.datY-Axe file_2.datY-Axe file_3.dat
1111
2242
3396
441624
5525120

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

The example plot


Second method - Using sprintf function

Another 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)