我成功地使用代码将动画保存为mp4格式
import matplotlibmatplotlib.use("Agg")~some codes~ani = animation.FuncAnimation(fig,draw,update,interval=10,blit=False)mywriter = animation.FFMpegWriter(fps=60)ani.save('myanimation.mp4',writer=mywriter)
但如果我将myanimation.mp4更改为gif格式,python会出错
Traceback (most recent call last): file "C:\Users\administrator\Desktop\edison\Edison_v4_backup_1\ver5.py",line 164,in <module> ani.save('demoanimation.gif',writer=mywriter); file "C:\Python27\lib\site-packages\matplotlib\animation.py",line 718,in save writer.grab_frame(**savefig_kwargs) file "C:\Python27\lib\site-packages\matplotlib\animation.py",line 204,in grab_frame dpi=self.dpi,**savefig_kwargs) file "C:\Python27\lib\site-packages\matplotlib\figure.py",line 1421,in savefig self.canvas.print_figure(*args,**kwargs) file "C:\Python27\lib\site-packages\matplotlib\backend_bases.py",line 2220,in print_figure **kwargs) file "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py",line 497,in print_raw renderer._renderer.write_rgba(filename_or_obj)RuntimeError: Error writing to file
看到我成功保存为mp4格式,我不知道为什么它在保存gif格式时会出错.
解决方法 这是因为没有外部程序,matplotlib不支持GIF.如果您正确安装和配置了imagemagick,则应该可以:import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltimport matplotlib.animationimport numpy as npdef init_animation(): global line line,= ax.plot(x,np.zeros_like(x)) ax.set_xlim(0,2*np.pi) ax.set_ylim(-1,1)def animate(i): line.set_ydata(np.sin(2*np.pi*i / 50)*np.sin(x)) return line,fig = plt.figure()ax = fig.add_subplot(111)x = np.linspace(0,2*np.pi,200)ani = matplotlib.animation.FuncAnimation(fig,animate,init_func=init_animation,frames=50)ani.save('/tmp/animation.gif',writer='imagemagick',fps=30)总结
以上是内存溢出为你收集整理的matplotlib在gif错误中保存动画全部内容,希望文章能够帮你解决matplotlib在gif错误中保存动画所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)