Gnuplotting

Create scientific plots using gnuplot

November 7th, 2012 | 2 Comments

Assume you have a data file describing a trajectory that you would like to animate like the spiral shown in Fig. 1.

moving spiral

Fig. 1 An animated spiral trajectory (code to produce this figure, data)

In order to create the animation we have to produce a set of png images and create the resulting gif animation with GIMP as shown in the Animation I – gif entry. Therefor, we have to tell gnuplot at which point of the data it has to stop for each image. This can be achieved by the every option. The point at the end of the line is just one data point. Here the start point and end point for every are just the same.

do for [ii=1:99] {
    splot 'spiral.txt' every ::1::ii w l ls 1, \
          'spiral.txt' every ::ii::ii w p ls 1
}

The downward spiral is created by running the loop in the other direction.

do for [ii=99:1:-1] {
    splot 'spiral.txt' every ::1::ii w l ls 1, \
          'spiral.txt' every ::ii::ii w p ls 1
}

By the way, I don’t know why the antialiasing of the output png images is not working in this example. If you have any idea, feel free to tell me.Assume you have a data file describing a trajectory that you would like to animate like the spiral shown in Fig. 1.

August 17th, 2012 | 7 Comments

As already mentioned gnuplot 4.6 overs an easier way to include loops in your code.
Here we are using it to create an animation of a set of head related impulse responses, which show differences in amplitude and arrival time at the left and right ear of a listener depending on the position of the source.

Fig. 1 Video animation of head related impulse responses (HRIRs) (code to produce this figure, data)

In comparison to the additional file for the loop in Animation I – gif, now all we need is this small code block.

do for [ii=1:181] {
    set output sprintf('hrir_frame%03.0f.png',ii)
    set multiplot layout 2,1
    [...]
    plot 'ir.txt' u ($1*1000):2*ii-1 w l ls 1
    [...]
    plot 'ir.txt' u ($1*1000):2*ii w l ls 1
    [...]
}

February 3rd, 2012 | 5 Comments

In the first post regarding animations we have created a bunch of png files and combined them to a single gif animation. Now we want to generate again a bunch of png files, but combine them to a movie.

Fig. 1 Video animation of Huygens principle (code to produce this figure, loop function)

We create the png files in analogy to the gif example, hence we will discuss only the generation of the movie here. In order to compose a avi file from the png files we are using Mencoder. Gnuplot is able to directly start Mencoder by its system command.

# Create movie with mencoder
ENCODER = system('which mencoder');
if (strlen(ENCODER)==0) print '=== mencoder not found ==='; exit
CMD = 'mencoder mf://animation/*.png -mf fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o huygen.avi'
system(CMD)

The first two lines check if Mencoder is available and quit gnuplot if not. The Mencoder command itselfs gets the directory containing the png files mf://animation/*.png, frames per second and input type-mf fps=25:type=png, video -ovc and audio -oac options, and finally of course the output file -o huygen.avi.

In order to generate a webm video file for a web site, ffmpeg can be used to convert the video.

$ ffmpeg -i huygen.avi huygen.webm

October 21st, 2011 | 9 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.