概述
调整后的例子来自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()
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')
总结
以上是编程之家为你收集整理的python – Matplotlib.animation:如何删除白边全部内容,希望文章能够帮你解决python – Matplotlib.animation:如何删除白边所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧