How to make animations with Gnuplot

This is how you do it:

set terminal gif animate delay 10

The delay 10 means 10*0.01 seconds between each image. Then, you just plot each subsequent image until your done. I normally write a bash script to do this sort of thing automatically, so here’s an example.


#!/bin/bash
MAX=100
echo "clear" > plot.gpi
echo "reset" >> plot.gpi
echo "set terminal gif animate delay 10" >> plot.gpi
echo "set output \"animate.gif\"" >> plot.gpi
echo "set isosample 40" >> plot.gpi
echo "set hidden3d" >> plot.gpi
for i in `seq 0 ${MAX}`
do
echo "splot sin(${i}*x/${MAX})*cos(${i}*y/${MAX})" \
>> plot.gpi
done

So what I’m doing here is letting a bash script create a gnuplot script for me. Instead of writing out about a hundred different splots by hand, I let a for loop do that for me. Additionally, I specify a few things at the beginning.
So, put that code in a file somwhere.
Make it executable:chmod +x file
Then run it: ./file
Now, you should have a file plot.gpi that contains all the gnuplot commands.
Now open gnuplot: gnuplot
Then run the plot.gpi script: gnuplot> load 'plot.gpi'
And now you should have an animated gif creatively titled animation.gif.

When I do this in real life, I normally have a data file (perhaps generated with octave/matlab) with each time step separated by two blank lines. After two blank lines, gnuplot says we are on a new index so I can make my repeated plot line as something like:
for i ...; do echo "plot 'datafile' using 1:2 index ${i} with lines"; done etc…

Tags: , ,

9 Responses to “How to make animations with Gnuplot”

  1. AlBundy Says:

    It’s great!.

    Thanks

  2. GregorSamsa Says:

    echo “set output animate.gif” >> plot.gpi Here is a mistake

    echo “set output \”animate.gi\”f” >> plot.gpi shuld be written

  3. GregorSamsa Says:

    echo “set output \”animate.gif\” ” >> plot.gpi Sorry my mistake

  4. bennett Says:

    Thanks. No other “how to” i found was as helpful as yours.

  5. miggysmith Says:

    Thanks Gregor!

  6. Trix0r Says:

    Very helpful, thanks! For my needs just enough.

    However, I got an error when running “load ‘plot.gpi'” in gnuplot using my mac:

    “Could not find/open font when opening font “Arial”, using internal non-scalable font”

    + Preview can’t open the resulting gif…

  7. Bing Says:

    thanks a lot. very helpful

  8. talis Says:

    thx a lot! gnuplot index made my day :)

  9. Sergio Says:

    Thanks a lot!
    How can I speed up the animation? the “delay” command does not seem to change anything…

Leave a comment