Search code examples
gnuplot

gnuplot gif image: is there a way to make title dynamic?


I'm trying to create a gif with the dynamic title that is taking column header for each frame.

The code:

set terminal gif animate delay 20
set output 'gse.gif'
set cbrange [0:0.00025]
set xrange [-0.3:0.3]
set yrange [-0.3:0.3]
set ylabel "z (m)"
set xlabel "x (m)"
set cblabel "grain size"
set rmargin at screen 0.8

do for [i=0:10] {
    plot 'gse.dat' index i with image title columnheader(1)
}

with the dataset:

"Time: 0 years"
-0.2475 -0.546333   1e-05
-0.2425 -0.546333   1e-05
-0.2375 -0.546333   1e-05
-0.2325 -0.546333   1e-05
-0.2275 -0.546333   1e-05
-0.2225 -0.546333   1e-05
-0.2175 -0.546333   1e-05
-0.2125 -0.546333   1e-05
...


"Time: 162 years"
-0.247513   -0.546305   1e-05
-0.242512   -0.546305   1e-05
-0.237512   -0.546305   1e-05
...


"Time: 1674 years"
-0.247631   -0.546045   1e-05
-0.242628   -0.546045   1e-05
-0.237625   -0.546045   1e-05
...

produces:

enter image description here

My hope is that it will set the title like this:

enter image description here

but with the value for each frame that is taken from the first column header.

P.S. I would also consider changing dataset format if that will be more convenient.


Solution

  • Sure, there is a way. You've been pretty close. Short answer would be: add a line, e.g.

    set key center tmargin

    The default position of the key/legend is "top right inside". However, I guess it is covered by the with image graph. So, if you set e.g. set key center tmargin you will see it above the graph. Check help key

    Script:

    ### animated gif with variable title
    reset session
    
    # create some test data
    set samples 11
    set isosamples 11
    set print $Data
        do for [t=0:10] {
            print sprintf('"Time: %d years"',t)
            do for [y=0:10] for [x=0:10] {
                print sprintf("%g %g %g",y,x,x*y+t*4)
            }
            print ""; print ""   # two empty lines
        }
    set print
    
    set xrange[:] noextend
    set yrange[:] noextend
    set cbrange[0:120]
    
    set term gif size 600,360 animate delay 40
    set output "SO73598093.gif"
    
    set key center tmargin
    
    do for [i=0:10] {
        plot $Data u 1:2:3 index i w image title columnheader(1)
    }
    set output
    ### end of script
    

    Result:

    enter image description here