使用matplotlib和ImageMagic导出gif

  工作中要用到Python的绘制动画功能,于是通过matplotlib库,运行了下面这个程序:

 1 import numpy as np
 2 from matplotlib import pyplot as plt
 3 from matplotlib import animation 6 
 7 X, Y = np.mgrid[:2*np.pi:0.2,:2*np.pi:0.2]
 8 U = np.cos(X)
 9 V = np.sin(Y)
10 
11 fig, ax = plt.subplots(1,1)
12 Q = ax.quiver(X, Y, U, V, pivot='mid', color='r', units='inches')
13 
14 ax.set_xlim(-1, 7)
15 ax.set_ylim(-1, 7)
16 
17 def update_quiver(num, Q, X, Y):
18     """updates the horizontal and vertical vector components by a
19     fixed increment on each frame
20     """
21 
22     U = np.cos(X + num*0.1)
23     V = np.sin(Y + num*0.1)
24 
25     Q.set_UVC(U,V)
26 
27     return Q,
28 
31 anim = animation.FuncAnimation(fig, update_quiver, fargs=(Q, X, Y),
32                                interval=10, blit=False)
33 
34 # plt.show()
35 anim.save('animation.gif', writer='imagemagick', fps=30, dpi=1000)

程序报错了,

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-2-552d081ed7ba> in <module>()
     31 
     32 # plt.show()
---> 33 anim.save('animation.gif', writer='imagemagick', fps=30, dpi=1000)

C:\Anaconda2\lib\site-packages\matplotlib\animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
    778                     # TODO: Need to see if turning off blit is really necessary
    779                     anim._draw_next_frame(d, blit=False)
--> 780                 writer.grab_frame(**savefig_kwargs)
    781 
    782         # Reconnect signal for first draw if necessary

C:\Anaconda2\lib\site-packages\matplotlib\animation.pyc in grab_frame(self, **savefig_kwargs)
    223             # frame format and dpi.
    224             self.fig.savefig(self._frame_sink(), format=self.frame_format,
--> 225                              dpi=self.dpi, **savefig_kwargs)
    226         except RuntimeError:
    227             out, err = self._proc.communicate()

C:\Anaconda2\lib\site-packages\matplotlib\figure.pyc in savefig(self, *args, **kwargs)
   1537             self.set_frameon(frameon)
   1538 
-> 1539         self.canvas.print_figure(*args, **kwargs)
   1540 
   1541         if frameon:

C:\Anaconda2\lib\site-packages\matplotlib\backends\backend_qt5agg.pyc in print_figure(self, *args, **kwargs)
    194 
    195     def print_figure(self, *args, **kwargs):
--> 196         FigureCanvasAgg.print_figure(self, *args, **kwargs)
    197         self.draw()
    198 

C:\Anaconda2\lib\site-packages\matplotlib\backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2228                 orientation=orientation,
   2229                 bbox_inches_restore=_bbox_inches_restore,
-> 2230                 **kwargs)
   2231         finally:
   2232             if bbox_inches and restore_bbox:

C:\Anaconda2\lib\site-packages\matplotlib\backends\backend_agg.pyc in print_raw(self, filename_or_obj, *args, **kwargs)
    517             close = False
    518         try:
--> 519             fileobj.write(renderer._renderer.buffer_rgba())
    520         finally:
    521             if close:

IOError: [Errno 22] Invalid argument

  经过多次搜索检查,原来通过matplotlib保存gif需要安装ImageMagic,这是它的下载地址:http://www.imagemagick.org/script/binary-releases.php#windows

安装完成后配置matplotlib,先看看配置文件放在哪儿了:

import matplotlib
print matplotlib.matplotlib_fname()

输出路径为: C:\Anaconda2\lib\site-packages\matplotlib\mpl-data\matplotlibrc,然后打开matplotlibrc文件,编辑末尾的:

animation.convert_path: '"C:\Program Files\ImageMagick-6.9.0-Q16\convert.exe"'

记得把取消“animation.convert_path”前面的注释。这样应该就配置好了,接下来继续运行开始的程序,发现还是出现同样的问题,在运行程序上添加两行:

import matplotlib
matplotlib.rcParams['animation.convert_path'] = 'C:\\Program Files\\ImageMagick-6.9.3-Q16\\convert.exe'

终于成功了。

 


posted on 2016-02-27 13:23  淙头  阅读(1989)  评论(0编辑  收藏  举报

导航