matplotlib使用animation绘图动态图
尝试了各种动态的制作方法,以下应该是最容易实现的
1 import matplotlib.pyplot as plt 2 import matplotlib.animation as ma 3 import numpy as np 4 5 #数据准备 6 x=np.arange(-np.pi,np.pi,0.1) 7 y=np.sin(x)*np.cos(x) 8 9 #创建fig和axes 10 fig,axes=plt.subplots(1,1) 11 12 #创建axes更新函数 13 def anima(i): 14 x_line,y_line=[],[] 15 x_line.extend(x[:i]) 16 y_line.extend(y[:i]) 17 axes.cla() 18 axes.plot(x_line,y_line,'ro') 19 20 #创建动图对象 FuncAnimaion(fig对象,动图更新函数,frames帧数,interval帧更新间隔默认200ms) 21 ani=ma.FuncAnimation(fig,anima,frames=len(y),interval=400) 22 23 #保存动图-需要下载imagemgick http://www.imagemagick.org/script/download.php 24 ani.save('test.gif',writer='imagemagick') 25 26 plt.show()
浙公网安备 33010602011771号