如何使用“matplotlib.animation”模块设置保存为 mp4 影片的动画的分辨率?
在网上我只找到了使用“animation.FuncAnimation”的示例。例如来自 http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/ 的精彩教程使用:
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'])
从 matplotlib.animation 模块引用中,我发现“animation.Animation.save”方法提供了“dpi”参数,但我不知道如何正确应用此函数
matplotlib.animation.Animation.save(filename, writer=None, fps=None, dpi=None, codec=None, bitrate=None, extra_args=None, metadata=None, extra_anim=None)
一个小示例代码可能会有所帮助。
非常感谢。
约翰内斯
PS:顺便问一下,如何使用 sytax-highlighting 插入 Python 代码?
最佳答案
您可以通过迂回方式控制分辨率。分辨率、图形大小和 dpi 并不都是独立的,如果你知道其中两个,那么第三个就固定了。
您可以在保存参数中设置dpi
,并在保存之前使用
fig.set_size_inches(w_in_inches, h_in_inches, True).
您的分辨率为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)
您可能需要从源代码升级到较新版本的 mpl(debian 很棒,因为它非常保守,也很糟糕,因为它非常保守)。
关于animation - 如何设置动画的图像分辨率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14666439/