Create scientific plots using gnuplot

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


  1. Xenofon Floros says:

    Can I ask a stupid question? I have the following problem.. I have a csv file with 2 columns with x (time) and y values of a trajectory.. I need to animate the trajectory as it evolves over time.. So in each frame i I suppose I have to plot all values until i and some marker in the end to denote the current position.. How can I adapt your code to do that? I cannot get it to work..

    Thanks a lot,

  2. hagen says:

    Great question, I have created an extra entry to answer it:

  3. Tüzes Dániel says:

    It’s regrettable that tabs are not usable for formatting the code, there’s only spaces for this purpose.
    Great post! Great site!

  4. Sami says:

    Hi, I did “brew uninstall gnuplot; brew install gnuplot –with-cairo”. I run “animate_ir.gnu” but the code seems to be in some infinite loop. I have the data ir.txt in the same working directory as the code. How much time did it took from you to run this code? I am running OSX Yosemite for this.

  5. Sami says:

    Everything works now! The output is in the working directory as image stream (113 images for me). Have you found a solution which produces directly a video file from these pictures directly? Even better would be – a direct output to the screen as video.

  6. Sami says:

    Your code has the answer also to my last question. The code is so intensive that it does not manage to create any video file because it still creating the images. I would like to change this approach by just directing the latest image to screen but keeping in memory the previous pictures such that the user can browse images in some interface. How can you reach such a model? I think qt can be a good option as a terminal.

  7. Sami says:

    I made a summary about what I am trying to accomplish with your code here I would like to have a continuous stream to some interface where the user can browse existing images, can do cheap deletions and later search of the morphology in images.

Leave a Reply