Gnuplot Basic plotting of data files Plotting multiple data files

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


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.


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


set terminal postscript color noenhanced ##setting the term
set output ""

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)

Got any Gnuplot Question?