Plotting trajectory with gnuplot

泄露秘密 提交于 2021-02-04 08:23:25

问题


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?


回答1:


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:




回答2:


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
}


来源:https://stackoverflow.com/questions/61715139/plotting-trajectory-with-gnuplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!