Gnuplotting

Create scientific plots using gnuplot

October 21st, 2011 | 11 Comments

For presentation or teaching purposes it can be useful to show not only a figure, but an animation to illustrate something more clear. There exists different possibilities to do this in Gnuplot. Today we will have a look at how to create a gif animation.

Bessel function

Fig. 1 Animation of Bessel function (code to produce this figure, loop)

Fig. 1 shows a gif animation of a Bessel function of first kind and zeroth order. Gnuplot has a gif output terminal, but the result will be smoother if we first create png files with Gnuplot and then the animated gif file with another program. In order to create a set of png files we have to loop through the time value t and different output files.

# initializing values for the loop and start the loop
t = 0
end_time = 1
system('mkdir -p animation')
load 'bessel.plt'

The above code sets a start value for the running time variable t and the end_time variable at which the looping should be stopped. Then a folder for the output png files is created and the loop file bessel.plt is loaded. The content of this loop file is shown below.

# bessel loop
t = t + 0.02
outfile = sprintf('animation/bessel%03.0f.png',50*t)
set output outfile
splot u*sin(v),u*cos(v),bessel(u,t) w pm3d ls 1
if(t<end_time) reread;

Here we update the time t, create a file name with a different number and plot out Bessel function. Finally the last line checks if our time t is smaller than the end_time variable, and executes the loop again if this is the case.

After we have created the png files in the animation folder, we start GIMP and load all the files as layers (File > Open as Layers). After this we save all layers together as an animated gif file.

11 Comments

  1. Fabian says:

    Amazing. Thank you very much for this article and the whole website.
    I’m absolutely fascinated of what you can get out of gnuplot. Thanks for sharing this knowledge with us.

  2. Sijo says:

    Thank you for sharing the information..
    Physicist’s rocks !!!
    I am trying to make an animation using GNU plot

  3. timeout says:

    cool, thanks for the info on the gif animations and the great site !

    even this is a nice one:
    add following line to the end of your script..although this requires imagemagick to be installed.
    system(‘convert -delay 10 -loop 0 animation/*.png animation/ani.gif’)

  4. hagen says:

    Thanks, this is indeed a nice one and gives you the same result as with GIMP.

  5. Nicolas says:

    I’ve been trying to do this with gnuplot 4.4 and I can’t get it. It only works with 4.6 ??

  6. hagen says:

    I have tested it with gnuplot 4.4.3, and 4.4.2. It both worked.
    With gnuplot 4.6 you can do it in an easier way, see Animation IV – trajectory.

  7. Youjun Hu says:

    Thank you for this article and the great website.
    It may be even better if we use the loop structure of gnuplot:

    do for [i=1,100] {
     }
    

    to replace the “reread”.
    In this case, only a single script file is needed.

  8. hagen says:

    Hi Youjun,

    indeed. The nice do loop is only available since gnuplot 4.6. This is the reason why older article are using strange ways to do similar things ;)

  9. Carlo says:

    I would like to create a gif file with variable delay (it’s about uniform convergence of functions, and it should speed up not to get too boring).

    since there are many frames, one probably should use command line …
    the following command works for fixed delay for instance
    convert -delay 20 -loop 0 *.jpg myimage.gif

    Any idea?

  10. Dedalus says:

    Hi. What if instead of the Bessel function, I have a 3D plot that evolves in time? I have some function of two spatial variables, and time for which I want to get the plot in different png files for each time. The data is printed in a file in columns, x,y, f(x,y) and this is done for each time. I could also print the time variable, like x,y,f(x,y),t. How do I tell to gnuplot to do a different plot for each t?

  11. […] stunning 3D animated gif images. Some nice examples can be found all over the web, such as this animated Bessel function, my own (very old) molecular d-and f-orbitals, or this collection. Once you finish creating a […]