我尝试使用matplotlib电影作家生成一部电影.如果我这样做,我总是在视频周围留下白色边缘.有谁知道如何删除该保证金?
调整后的例子来自http://matplotlib.org/examples/animation/moviewriter.html
# This example uses a MovieWriter directly to grab individual frames and # write them to a file. This avoids any event loop integration,but has # the advantage of working with even the Agg backend. This is not recommended # for use in an interactive setting. # -*- noplot -*- import numpy as np import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import matplotlib.animation as manimation FFMpegWriter = manimation.writers['ffmpeg'] Metadata = dict(title='Movie Test',artist='Matplotlib',comment='Movie support!') writer = FFMpegWriter(fps=15,Metadata=Metadata,extra_args=['-vcodec','libx264']) fig = plt.figure() ax = plt.subplot(111) plt.axis('off') fig.subplots_adjust(left=None,bottom=None,right=None,wspace=None,hspace=None) ax.set_frame_on(False) ax.set_xticks([]) ax.set_yticks([]) plt.axis('off') with writer.saving(fig,"writer_test.mp4",100): for i in range(100): mat = np.random.random((100,100)) ax.imshow(mat,interpolation='nearest') writer.grab_frame()
解决方法
传递无作为subplots_adjust的争论不会做你认为它做的事情
(doc).这意味着’使用deault值’.要做你想做的事,请使用以下代码:
fig.subplots_adjust(left=0,bottom=0,right=1,top=1,hspace=None)
如果重新使用ImageAxes对象,也可以使代码更有效
mat = np.random.random((100,100)) im = ax.imshow(mat,interpolation='nearest') with writer.saving(fig,100)) im.set_data(mat) writer.grab_frame()
默认情况下,imshow将纵横比固定为相等,即像素为正方形.您需要将图形重新调整为与图像相同的宽高比:
fig.set_size_inches(w,h,forward=True)
或告诉imshow使用任意宽高比
im = ax.imshow(...,aspect='auto')