工做中要用到Python的繪製動畫功能,因而經過matplotlib庫,運行了下面這個程序:php
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)
程序報錯了,python
--------------------------------------------------------------------------- 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#windowscanvas
安裝完成後配置matplotlib,先看看配置文件放在哪兒了:windows
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」前面的註釋。這樣應該就配置好了,接下來繼續運行開始的程序,發現仍是出現一樣的問題,在運行程序上添加兩行:ui
import matplotlib matplotlib.rcParams['animation.convert_path'] = 'C:\\Program Files\\ImageMagick-6.9.3-Q16\\convert.exe'
終於成功了。spa