17

how can I set the resolution of an animation saved as mp4 movie with "matplotlib.animation" module?

On the web I only found examples using "animation.FuncAnimation". For example the nice tutorial from http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/ used:

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20, blit=True)
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

From the matplotlib.animation module reference I found the "animation.Animation.save" method providing a "dpi"-argument, but I don't know how to apply this function properly

matplotlib.animation.Animation.save(filename, writer=None, fps=None, dpi=None, codec=None, bitrate=None, extra_args=None, metadata=None, extra_anim=None)

A small example code may be helpful.

Many thanks.

Johannes

PS: By the way, how can you insert Python-code with sytax-highlighting?

mr_endres
  • 336
  • 1
  • 2
  • 5
  • The highlighting is auto-magical. Also note that the animation module is relatively, so you have to check that your version of `matplotlib` matches the version of examples you find. – tacaswell Feb 02 '13 at 21:26
  • Did you get this sorted out? – tacaswell Oct 05 '13 at 01:01

2 Answers2

22

You can control the resolution in a round-about way. The resolution, figure size, and dpi are not all independent, if you know two of them, then the third is fixed.

You can set the dpi in the save argument, and before you save it, set the size of the figure with

fig.set_size_inches(w_in_inches, h_in_inches, True). 

Your resolution is then dpi * w_in_inches X dpi * h_in_inches.

dpi = 100
writer = animation.writers['ffmpeg'](fps=30)
ani.save('test.mp4',writer=writer,dpi=dpi)

You may need do upgrade to a newer version of mpl (debian is great because it is so conservative and awful because it is so conservative) from source.

Ben
  • 53
  • 9
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Hmm ok, but the same TypeError as with the "bitrate"-argument: `anim = animation.FuncAnimation(fig, update, init_func=init, frames=len(t), blit=True) animation.Animation.save(anim,'toll.mp4', fps=20, dpi=800, codec='mp4')` I get the error "save() got an unexpected keyword argument 'dpi'" – mr_endres Feb 02 '13 at 21:56
  • Ok many thanks, now it is working, the "figure.set_dpi()" method has no effect, but calling `fig.set_size_inches(h_in_inches, w_in_inches)` results in an image resolution of "h_in_inches xw_in_inches". – mr_endres Feb 03 '13 at 09:40
  • @mr_endres what version of MPL are you using? – tacaswell Feb 03 '13 at 14:36
  • Newest version from Debian Sid: 1.1.1~rc2-1 – mr_endres Feb 05 '13 at 18:43
3

bitrate is the parameter used to specify the quality of a movie. The higher the value you set it to, the higher the quality of the movie will be.

David
  • 877
  • 1
  • 7
  • 18
  • But if I ran the above example with `anim = animation.FuncAnimation(fig, update, init_func=init, frames=len(t), blit=True) animation.Animation.save(anim,'toll.mp4', fps=20, bitrate=20, codec='mp4')` I got the error "save() got an unexpected keyword argument 'bitrate'". – mr_endres Feb 02 '13 at 21:06
  • 2
    I think `bitrate` (and maybe `dpi`) were added circa MPL 1.2. – Nick T Apr 09 '13 at 22:22
  • Works for me now with mpl 1.3. Also, amazing answer, this was the reason my movies where not displaying properly and taking forever to save as well. – Justin Furuness Oct 03 '20 at 07:39