5

Is it possible to output sequence of files in vector format through matplotlib animation module?

For example:

...
anim = animation.FuncAnimation(...)
anim.save('animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

outputs mp4 movie (through ffmpeg).

Can I instruct animation class to output sequence of vector files?

theta
  • 24,593
  • 37
  • 119
  • 159

1 Answers1

1

To see what writers are available on your system use:

import matplotlib.animation as animation
print animation.writers.list()

You might need to write a new writer for what you want.

Edit

"FileWriters" from Matplotlib animation module seem to support only raster output. For FFMpegFileWriter() that's clear from documentation, and for FileMovieWriter() it can be derived if we initiate this class with unsupported format:

plt.rcParams['animation.frame_format'] = 'svg'

in which case Error is raised printing supported formats:

Unrecognized animation.frame_format string "'svg'": valid strings are ['rgba', 'tiff', 'jpeg', 'png', 'raw']

So Matplotlib animation module seems to work only with raster data.

Amar
  • 11,930
  • 5
  • 50
  • 73
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • It outputs `['ffmpeg', 'ffmpeg_file']`. I looked in dense list of codecs supported by ffmpeg, but none seems to provide vector output, and I'm not sure in the first place if MPL feeds the encoder with raster or is there any possibility for vector output from MPL animation module. My problem is that I used x264 with custom parameters suited for animation, but my contour animation can look better for sure. Maybe I should forget about it, and use `figsave()` then employ some animation package. – theta May 30 '13 at 18:55
  • you just need to get the parameters right. The animation writers are _exactly_ the same thing as saving the images using `figsave` (in fact 'ffmpeg_file' _does_ save the files and then uses ffmpeg to stitch them together, but it is way slow). – tacaswell May 30 '13 at 18:59
  • 1
    Perhaps [FileMovieWriter](http://matplotlib.org/api/animation_api.html#matplotlib.animation.FileMovieWriter) can output sequence of vector files. I'll look into it, as I'm under impression that animation module is much faster then using simply `figsave()` in a loop. – theta May 30 '13 at 18:59
  • it is faster because it just pipes the rendered image directly into `ffmpeg` with out writing a file to disk (including all the meta data). I don't know of any movie format that is vector based (but I don't know a lot about video codecs). – tacaswell May 30 '13 at 19:01
  • I tried various parameters, then finally I followed Dark Shikari's advice on encoding animation with x264 - it looks OK, but vector animation can do it better. – theta May 30 '13 at 19:02
  • SWF is vector format provided by popular animation packages, also there is SVG animation but that's too fresh. But ffmpeg SWF encoder works just with raster – theta May 30 '13 at 19:03
  • 1
    I thought SWF was flash and basically dead. – tacaswell May 30 '13 at 19:05
  • Not at all. Any decent vector based animation application writes vector to SWF or doesn't write at all. HTML5 and SVG animation is too experimental. libming and swftools provide Python bindings, but I never really used them. – theta May 30 '13 at 19:08
  • If you want to write a SWF writer, I suspect the devs would be appreciative, but I think the answer to your original question is no. – tacaswell May 30 '13 at 19:11
  • Well I imagine SWF writer to be very complex, but saving the sequence of vector files would let us use in 3rd party animation package and assemble output. However I just finished playing with `FileMovieWriter()` class, which unfortunately doesn't accept vector format, just like `FFMpegFileWriter()` doesn't too. Supported formats by MPL animation module seem to be these: `['rgba', 'tiff', 'jpeg', 'png', 'raw']` which is printed after initializing `FileMovieWriter()` with unsupported format (ie. `rcParams['animation.frame_format'] = 'svg'`). If you could update your answer it would be great. Thx – theta May 30 '13 at 19:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30936/discussion-between-tcaswell-and-theta) – tacaswell May 30 '13 at 19:39