首页 > 解决方案 > 如何在 gnuplot 中为数据文件设置动画?

问题描述

我有一个文件,其中包含时间 t 瞬间双摆的坐标 (x1, y1), (x2, y2)。我想知道如何为这个文件制作动画。

n   t   x   y
1. 0. 0.3435957874318018 -0.36323812418181006
2. 0. 0.3490554835456995 -0.8632083150115673
1. 0.03 0.3299928793095484 -0.3756390549516834
2. 0.03 0.34657472136383705 -0.8753640218225479
1. 0.06 0.3099940098067639 -0.3923056383534705
2. 0.06 0.3437105896402516 -0.891167535319286

这是双摆的前三个位置,'n'是粒子数

标签: animationgnuplotpendulum

解决方案


以下可能是一个起点。由于我没有足够的数据来说明,我生成了一些数据(注意:它不是“真实的”双摆数据)。假设锚点是 a at 0,0。此外,可能不仅需要绘制点with points,还需要绘制钟摆的条with vectors。如何绘制第二个柱有点棘手且不明显,因为您必须记住 mass1 的先前位置。我希望你能弄清楚代码是如何工作的。

代码:

### animate double pendulum
reset session

# create some test data (no "real" double pendulum data, just for illustration)
set print $Data
    do for [t=0:15] {
        print sprintf("%d. %g %g %g", 1, t, x1=cos(t*pi/20+pi/8), y1=-sin(t*pi/20+pi/8))
        print sprintf("%d. %g %g %g", 2, t, x2=x1+cos(t/10.), y2=y1-sin(t/10.))
    }
set print

set size ratio -1
set xrange[-1.5:2.0]
set yrange[-2:0.1]

set term gif size 400,300 animate delay 20
set output "DoublePendulum.gif"
do for [i=0:15] {
    plot $Data u (0):(0):3:4 every ::2*i::2*i w vectors lc "grey" nohead notitle, \
         x2=y2=NaN '' u (x1=x2,x2=$3):(y1=y2,y2=$4):(x1-x2):(int($0)%2==0 ? NaN: y1-y2) every ::2*i::2*i+1 w vectors lc "grey" nohead notitle, \
         ''    u 3:4 every ::2*i::2*i     w p pt 7 ps 3 lc "red" title "1", \
         ''    u 3:4 every ::2*i+1::2*i+1 w p pt 7 ps 2 lc "blue" title "2", \
}
set output

# just for illustration: plot all positions at the same time
set term wxt   #  or any other terminal
plot $Data u (0):(0):3:4 every 2 w vectors lc "grey" nohead notitle, \
     x2=y2=NaN '' u (x1=x2,x2=$3):(y1=y2,y2=$4):(x1-x2):(int($0)%2==0 ? NaN: y1-y2) w vectors lc "grey" nohead notitle, \
     ''    u 3:4 every 2    w p pt 7 ps 3 lc "red" title "1", \
     ''    u 3:4 every 2::1 w p pt 7 ps 2 lc "blue" title "2", \
### end of code

结果:(没有“真实的”双摆数据,仅用于说明)

在此处输入图像描述

所有职位(在 wxt 终端中):

在此处输入图像描述


推荐阅读