Search code examples
gnuplot

Plotting trajectory with gnuplot


I have a datafile with position of a moving point in the following format.

x1  y1
x2  y2 
x3  y3
.
.
.

I wish to make an animated trajectory with this data in gnuplot. How can I do that?

I tried

do for [i=1:20] {
plot "temp.dat" every ::i using 1 : 2 w p
}

But it plots all the points in a single image, not an animation. What is the way of doing this?


Solution

  • While I was coding and got interrupted... @Ethan's answer already contains all the necessary ingredients, I post my answer nevertheless, with a little visual demo... Check help gif, help stats and help every, these are the main "components". In the following example you hopefully find what you are looking for.

    Code:

    ### trajectory animated
    reset session
    
    # create some test data
    v = 40
    a = 45
    g = 9.81
    set print $Data
        do for [i=0:86] {
            t = i/10.
            sx(t) = v*cos(a)*t
            sy(t) = v*sin(a)*t - 0.5*g*t**2 
            print sprintf("%.3f %.3f",sx(t),sy(t))
        }
    set print
    
    set xrange[0:200]
    set yrange[0:80]
    
    set term gif size 400,300 animate delay 5 optimize
    set output "Trajectory.gif"
    
    stats $Data nooutput
    N = STATS_records
    do for [i=0:N-1] {
        plot $Data u 1:2 every ::::i w l notitle, \
             '' u 1:2 every ::i::i w p pt 7 lc rgb "red" notitle
    }
    set output
    ### end of code
    

    Result:

    enter image description here