Animation With Gnuplot. Animating Pendulum

Gnuplot can be used to produce animations in gif format. You only need to choose the appropriate terminal:

set terminal gif animate delay 4

Here delay 4 means that there will be 0.01 × 4 seconds between frames in your animation (1 / 0.04 = 25 frames per second). But what is the frame in gnuplot? Basically it is a plot command, so for 100 frames you need 100 plot commands. For example the following code

set terminal gif animate delay 4
set output "TrigAnimation.gif"
set xrange [-3:3]
set yrange [-3:3]

plot 1.0 * sin(x)
plot 0.9 * sin(x)
plot 0.8 * sin(x)
plot 0.7 * sin(x)
plot 0.6 * sin(x)
plot 0.5 * sin(x)
plot 0.4 * sin(x)
plot 0.3 * sin(x)
plot 0.2 * sin(x)
plot 0.1 * sin(x)
plot 0.0 * sin(x)
plot -0.1 * sin(x)
plot -0.2 * sin(x)
plot -0.3 * sin(x)
plot -0.4 * sin(x)
plot -0.5 * sin(x)
plot -0.6 * sin(x)
plot -0.7 * sin(x)
plot -0.8 * sin(x)
plot -0.9 * sin(x)
plot -1.0 * sin(x)
plot -1.0 * sin(x)
plot -0.9 * sin(x)
plot -0.8 * sin(x)
plot -0.7 * sin(x)
plot -0.6 * sin(x)
plot -0.5 * sin(x)
plot -0.4 * sin(x)
plot -0.3 * sin(x)
plot -0.2 * sin(x)
plot -0.1 * sin(x)
plot 0.0 * sin(x)
plot 0.1 * sin(x)
plot 0.2 * sin(x)
plot 0.3 * sin(x)
plot 0.4 * sin(x)
plot 0.5 * sin(x)
plot 0.6 * sin(x)
plot 0.7 * sin(x)
plot 0.8 * sin(x)
plot 0.9 * sin(x)
plot 1.0 * sin(x)

produces this animation:

Obviously such input files should be generated by some script. You can use any language you like, as for me I prefer Haskell.

Gnuplot is not very good at optimization, the resulting gif’s prove to be quite large. Use gifsicle to improve the output:

gifsicle -O TrigAnimation.gif -o TrigAnimationOpt.gif

Animating pendulum.

It is a common situation when your graphics is presented by a collection of points rather than a combination of elementary functions. Data files are used in a case of static graphics, but it is unpractical to have hundreds of files to make an animation. Fortunately gnuplot allows to embed data right into the plot files. Gnuplot provides some dummy file names to use in the plot command, ‘-‘ is among them. Read more in the gnuplot documentation.

Now I show you how to create a simple animation of a pendulum. The pendulum is represented as a two-point curve (thus not a curve actually). The code should be self-explanatory:

set terminal gif animate delay 4
set size ratio -1
set output "1.gif"
set xrange [-1:1]
set yrange [-1.5:0.5]
set nokey
unset xtics
unset ytics
unset border

plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.479425538604203 -0.8775825618903728
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4780215428037317 -0.8783481112950264
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4738112432235755 -0.880626428058419
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.466800091497177 -0.8843628636358648
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4569984452008731 -0.8894674929889145
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4444233976367903 -0.8958168583103197
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4291011875307727 -0.9032564258612726
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.411070043719281 -0.9116037621448413
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.39038328875162626 -0.9206524251113796
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.3671125020501049 -0.9301765482092696
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.34135052959363993 -0.9399360701378268
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.3132141245325882 -0.9496825323197665
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.28284601279603805 -0.9591653314446803
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.25041620009002624 -0.9681382787249309
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.21612237137479443 -0.9763662840304019
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.18018927968938878 -0.9836319552988401
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.14286707582542332 -0.9897418848594277
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.10442859068998299 -0.9945323873290924
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  6.516564435982236e-2 -0.9978744604383706
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  2.538451549059462e-2 -0.9996777612677537
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -1.4599242492294932e-2 -0.9998934253802508
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -5.446641018931241e-2 -0.9985156033638581
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -9.389988024412708e-2 -0.9955816453160126
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.13259033778420903 -0.991170924879392
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.17024158285333169 -0.9854023561305261
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.20657525765157456 -0.9784307144229404
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.24133477963819308 -0.9704419220834314
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.2742883317339307 -0.9616474983457385
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.30523081908567273 -0.9522783978859802
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.33398476377996117 -0.9425784728938188
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.36040017012922443 -0.9327977901832883
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.38435344967278007 -0.9231860190257615
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.40574554346496705 -0.9139860797399042
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4244994170709183 -0.9054282107966652
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4405571294313594 -0.897724576753472
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4538766898188645 -0.8910645040843397
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.46442891776070894 -0.8856103998641934
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4721945099925054 -0.8814943815662909
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.47716149765074795 -0.8788156263743239
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.47932324770880824 -0.8776384359210121
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4786771269229756 -0.8779910068792081
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4752239060969425 -0.879864898194015
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.46896793905072987 -0.8832151901674421
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4599181059834833 -0.8879613368770993
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4480894666027533 -0.8939887191232673
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4335055261209907 -0.9011509079075286
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.4162009787116262 -0.9092726463055426
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.39622476005888 -0.9181535489863789
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.3736432150517507 -0.9275725027434735
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.34854317024090886 -0.9372927282756529
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.3210346949893786 -0.9470674340368148
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.29125334152958837 -0.9566459590924162
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.2593616730493598 -0.9657802661853456
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.22554992036894558 -0.9742316117954509
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.19003565073796863 -0.9817771903281298
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.15306238479954642 -0.9882165280744274
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -0.11489715692351263 -0.9933773921983999
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -7.582707619422498e-2 -0.9971209828881524
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  -3.615500611251265e-2 -0.9993461940353824
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  3.805463882176116e-3 -0.9999927591961061
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  4.373553683599171e-2 -0.9990431436217695
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  8.331696676262787e-2 -0.996523097097842
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.12223779166032502 -0.992500842463122
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.16019777720348488 -0.98708493665898
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.1969133260708594 -0.9804209004380269
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.23212164232239355 -0.9726867651847406
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.26558398755025286 -0.9640877271062562
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.2970879220015215 -0.9548501278215434
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.3264484852335553 -0.9452149948496994
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.3535083327039177 -0.9354313757346908
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.37813690285309487 -0.9257496868488096
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.40022874013637655 -0.9164152746265466
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.419701140356899 -0.9076623561562519
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4364913137554178 -0.8997084711261029
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4505532778015728 -0.8927495415077281
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.46185469556473024 -0.8869556021497415
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.470373867702006 -0.8824672371159485
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4760970677973522 -0.8793927348089495
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4790163836587343 -0.877805960441434
e
plot '-' with linespoints lw 1 pt 7 ps 1 lc rgb "black"
  0 0
  0.4791281930259268 -0.8777449371245102
e

After the optimization with gifsicle we finally get

Advertisements
This entry was posted in Tips and tagged , . Bookmark the permalink.

4 Responses to Animation With Gnuplot. Animating Pendulum

  1. tapsanit says:

    \\\\\\\\\\Thanks for the useful trick

  2. QJl says:

    I think since one can use command “reread” to create a loop in gunplot, without a programming language the animation can also be created easily. For example, the first example can be realized like this:
    #########
    set terminal gif animate delay 4
    set output “TrigAnimation.gif”
    set xrange [-3:3]
    set yrange [-3:3]
    n=21
    i=0
    load “animate.gnuplot”
    #########
    with “animate.gnuplot” reading:
    #########
    plot (-1.+i*0.1)*sin(x)
    i=i+1
    if (i < n) reread
    #########

  3. cirosantilli says:

    check out `do for` loop

  4. Pingback: Python:Looking for library/tool to visualise multi-dimensional data [closed] – IT Sprite

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s