Sunday, May 15, 2011

Creating animated SVG plots with GNU Octave and gnuplot

It's relatively easy to create an animated plot as either a video (AVI, MPEG, ...) file or an animated GIF file via GNU Octave or gnuplot -- one needs only to create a sequence of images and then use a tool like mencoder or ffmpeg to combine these into a single video file.

But how about an animated SVG file? As plots are vector by nature, an animated vector file sounds cool. Gnuplot is capable of producing output graphs in SVG format, but I was not successful in finding a way of converting these into a single animated SVG.

So I hacked together something for the task. If you have a recent-enough browser (tested in Firefox 4.0.1 and Chromium 11.0.696.65) you can have a look at a trivial example.
If you're interested, grab the scripts here: ani-plot

3 comments:

  1. hello i have to rapresent a cellular automata, in spec the game of life and the ant of langton, but i'm search a way to rapresent it without other tool of gnuplot, have you find another way in this time without use external python programs?

    another thing: what is the best way to rapresent a grid with black square in it?

    ReplyDelete
  2. I tried your scripts on an Archlinux GNU/Linux system, 64 bit. First, I needed to install epstool, and then I installed xfig and transfig, to be sure to have fig2dev. The first script worked, developed the 62 plots. But the second script failed to incorporate them into an animation.

    Any ideas?

    ReplyDelete
    Replies
    1. Hi Alan. I haven't touched the scripts in a long time. I published it in a moment I was too happy over getting it to work, but it's just a hack. If gnuplot changes the structure of the generated SVG, the tool will just explode.
      Nevertheless, I tried it now on my Debian box and it works. But there are some dependencies, which I did not specify. You already discovered some.
      By first and second script I assume you refer to the example included in the archive. First part just generated the SVG images. And then the tool attempts to combine and animate them.
      When I ran the script here it ended up with: "ImportError: No module named lxml". The python script uses lxml to manipulate the SVG DOM tree. In Debian I resolved by installing python-lxml. Then it worked.
      FYI I have gnuplot 4.6 patchlevel 3. It is possible that it does not work because of gnuplot output change.
      What error message do you see when the script fails?

      Delete