How to set the image resolution for animations?

2020-08-13 05:37发布

问题:

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?

回答1:

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.



回答2:

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.