0

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

If you just want the animation to show on the screen, then your code is fine except that you need to add a delay between the successive frames:

do for [i=1:20] {
    plot "temp.dat" every ::i using 1 : 2 w p
    pause 0.1   # 1/10 second between frames
}

If you are making an animated gif file then the pause doesn't go into the loop itself, it becomes a parameter to the set term command:

set term gif animate delay 10    # 10 = 10 units of 0.01 seconds
set output 'animation.gif'
do for [i=1:20] {
    plot "temp.dat" every ::i using 1 : 2 w p
}

2 Comments

The first part is not working actually. I am getting all points in a single plot instead of an animation.
The command you show is legal, but may not produce what you actually wanted.As written it should generate a plot starting at point 1, a plot starting at point 2, and so on. If you want a single point per plot, maybe you want every ::i::i which says to start and stop at point i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.