Search code examples
labelgnuplot

How to add time dependent labels in Gnuplot


A datafile contains three columns with the first and second representing the x & y position of a circle at a time (in seconds) given by the third column. For example, the first two lines in "data.txt" give the position of two circles at time=0, followed by two blank lines then the position of the two circles at time=0.1 sec and so forth. The first few lines of data.txt are:

 0   0  0
-1  -1  0


 1  1.0 0.1
-1 -0.5 0.1


 1.2  1.25 0.2
-0.5 -0.25 0.2
...

The Gnuplot code producing a series of frames (a movie) with the position of the two circles in time is:

        set terminal gif size 1200,1200 animate delay 500
        set output "movie.gif"
        stats "data.txt" u 1:2 name "A"
        set style circle radius graph 0.025; set style fill solid
        set xrange [A_min_x*1.1:A_max_x*1.1]
        set yrange [A_min_y*1.1:A_max_y*1.1]
        do for [i=0:A_blocks-2] {
        plot "data.txt" index i u 1:2 w circle
        }

I'm trying to add a label or textbox of the form "Time=?" to each frame where the question mark is replaced by the number from the third column. Any suggestions on how to do it?


Solution

  • The first (not too obvious) solution which comes to my mind: plot your data in a loop and assign the value of the 3rd column to a variable, e.g. t. Use keyenty to print the legend with your time re-using the variable t. In order to avoid a symbol in the legend use plotting style with points and a point with pointsize 0.

    Code:

    ### animation with time label
    reset session
    
    $Data <<EOD
     0   0  0
    -1  -1  0
    
    
     1  1.0 0.1
    -1 -0.5 0.1
    
    
     1.2  1.25 0.2
    -0.5 -0.25 0.2
    EOD
    
    set terminal gif size 400,400 animate delay 100
    set output "SO70474478.gif"
    
    stats $Data u 1:2 name "A" nooutput
    set style circle radius graph 0.025; set style fill solid
    set xrange [A_min_x*1.1:A_max_x*1.1]
    set yrange [A_min_y*1.1:A_max_y*1.1]
    set key top left
    
    do for [i=0:A_blocks-1] {
        plot $Data index i u 1:(t=$3,$2) w circle notitle, \
             keyentry w points ps 0 ti sprintf("Time: %.1f",t)
    }
    set output
    ### end of code
    

    Result:

    enter image description here