Thursday, September 22, 2011

Creating gif animation using gnuplot

Now the new version of gnuplot(gnuplot4.6) makes things much easier, you can refer to another of my posts for the new way.

Some times an animation makes thing much easier to be understood. When I was in middle school, I can not understand the propagation of wave well although the teacher took great effort to explain it to me. At last he show me an animation of propagating wave, at the instance i understand it. So can gnuplot create such a powerful thing? The answer is yes. Under gif terminal, if the animate option is selected, Gnuplot will thought each plot as a frame of an animation. Next is an example.
#We will plot sin(x+t) in this gif animation
set term gif animate
set output "animate.gif"
n=24    #n frames
set xrange [0:4*pi]
load "animate.gnuplot"
set output
where file animate.gnuplot contain the following gnuplot commands:
plot sin(x+i*dt) w l lt 1 lw 1.5 title sprintf("t=%i",i)
if (i < n) reread
Note you can not use "plot for ..." to create an animation, because gnuplot will look it as a single plot and will only create a single frame. Run the script we get an animation as this.


  1. Hello,
    This is what I've been looking for while I googled it. Thanks !

  2. Excellent!! But isn't "show output" at the end necessary? I was getting GIF errors without that.

  3. That's really cool. Is there a way to animate a series of still images together using gnuplot though?

  4. thanks can you also put some examples of animation surfaces

  5. The above code shows how to obtain an animation of a function. The following code shows how to obtain an animation from a file:
    set xlabel "X,ang."
    set zlabel "Z, ang."
    p 'tin' index 0:i:1 u 2:4 w l lw 4 lt -1 t 'C' , 'tin' index 0:i:1 u 5:7 w l lw 4 lt 1 t 'O1' , 'tin' index 0:i:1 u 8:10 w l lw 4 lt 2 t 'O2'
    i =i + 1
    if (i < n ) reread
    The index 0:i:1 reads from the first line to the ith line of the file, 'tin'. The file is structured so that each data line is separated by two blank lines.


Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.