Gnuplot Animated GIF Example

An animated GIF turns a sequence of gnuplot frames into a short looping movie, which is the simplest way to show how a curve evolves over time.

set terminal gif animate delay 10 size 640,480
set output "wave.gif"
set xrange [-2*pi:2*pi]
set yrange [-1.4:1.4]
set xlabel "x"
set ylabel "sin(x + phase)"
do for [frame=0:24] {
    phase = frame * 2 * pi / 25
    set title sprintf("Traveling Wave, Frame %d of 25", frame + 1)
    plot sin(x + phase) with lines linewidth 2 linecolor rgb "#5e2d79" notitle
}
set output

How the script works

set terminal gif animate selects the animated GIF terminal: every plot command after set output appends one frame to wave.gif instead of replacing the picture. delay 10 sets the frame delay in hundredths of a second (10 = 0.1 s per frame), and size 640,480 fixes the canvas.

The do for [frame=0:24] { ... } loop runs its block 25 times with frame counting from 0 to 24. Inside, phase advances by one twenty-fifth of a full cycle per frame, so the sine wave appears to travel sideways and loops seamlessly. sprintf builds each frame’s title from the loop variable. Fixing xrange and yrange before the loop is essential — autoscaling per frame would make the axes jump around.

The final set output with no argument closes the file and finishes the animation. Note on terminals: animated GIF output is a desktop gnuplot feature — the browser-based online terminal does not provide the gif animate terminal, so this script runs best in a local install.

Run it online: Paste this script into the online terminal and run it — no install needed.

This animation example needs the desktop gif animate terminal, so for the full effect run it in a local install from the download page.

Variant to try

Animate a growing dataset instead of a function: keep the same terminal lines and, inside the loop, plot a data file with an every limit that grows with the frame counter, for example plot "series.dat" using 1:2 every ::1::(frame*5) with linespoints, so each frame reveals more points. For smoother motion, raise the frame count and lower the phase step proportionally.

Related tutorials: Your First Gnuplot Plot, Multiplot Layouts in Gnuplot. See more copy-and-run scripts in the Gnuplot Examples index.

Scripts on this page are adapted from the official gnuplot 6.0 demo collection (gnuplot.info) and rewritten for this tutorial.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *