GNUPLOT LOOP to generate many figures.

I want to do this for a long time but failed all the time. So basically, I built one GNUPLOT script and use sed command to generate new script and run GNUPLOT in BASH. This works well, but it’s tricky to type \” in BASH script which is essential in GNUPLOT script for file name, title or axial name, etc.

I used some time today to figure out how to do this in GNUPLOT. The code is compact and simple. I add an example below, it’s easy to understand. I have 300 files. a_1.dat … a_100.dat, b_1.dat … b_100.dat, c_1.dat … c_100.dat. And I want to generate 100 png figures. Each figure contain a, b, c three plots. I shifted the y axis for b and c to avoid the overlap of three plots. And output file has name file1.png … file100.png.

set grid
set xr [*:]
set yr [0:20]
set terminal pngcairo
set xlabel "x axis"
set ylabel "y axis"

do for [t=1:100]{
        outfile = sprintf('file%i.png',t)
        file1 = sprintf('a_%i.dat',t)
        file2 = sprintf('b_%i.dat',t)
        file3 = sprintf('c_%i.dat',t)
        set output outfile
        p file1 u ($1):2 w l ti "A", \
        file2 u ($1):($2+6) w l ti "B", \
        file3 u ($1):($2+10) w l ti "C"
        set output

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s