matplotlib绘制动画的示例matplotlib从1.1.0版本以后就开始支持绘制动画下面是几个的示例:第一个例子使用generator,每隔两秒,就运行函数data_gen:[python]viewplaincopyprint?1.#-*-coding:utf-8-*-2.3.importnumpyasnp4.importmatplotlib.pyplotasplt5.importmatplotlib.animationasanimation6.7.fig=plt.figure()8.axes1=fig.add_subplot(111)9.line,=axes1.plot(np.random.rand(10))10.11.#因为update的参数是调用函数data_gen,所以第一个默认参数不能是framenum12.defupdate(data):13.line.set_ydata(data)14.returnline,15.#每次生成10个随机数据16.defdata_gen():17.whileTrue:18.yieldnp.random.rand(10)19.20.ani=animation.FuncAnimation(fig,update,data_gen,interval=2*1000)21.plt.show()第二个例子使用list(metric),每次从metric中取一行数据作为参数送入update中:[python]viewplaincopyprint?1.importnumpyasnp2.importmatplotlib.pyplotasplt3.importmatplotlib.animationasanimation4.5.start=[1,0.18,0.63,0.29,0.03,0.24,0.86,0.07,0.58,0]6.7.metric=[[0.03,0.86,0.65,0.34,0.34,0.02,0.22,0.74,0.66,0.65],8.[0.43,0.18,0.63,0.29,0.03,0.24,0.86,0.07,0.58,0.55],9.[0.66,0.75,0.01,0.94,0.72,0.77,0.20,0.66,0.81,0.52]10.]11.12.fig=plt.figure()13.window=fig.add_subplot(111)14.line,=window.plot(start)15.#如果是参数是list,则默认每次取list中的一个元素,即metric[0],metric[1],...16.defupdate(data):17.line.set_ydata(data)18.returnline,19.20.ani=animation.FuncAnimation(fig,update,metric,interval=2*1000)21.plt.show()第三个例子:[python]viewplaincopyprint?1.importnumpyasnp2.frommatplotlibimportpyplotasplt3.frommatplotlibimportanimation4.5.#Firstsetupthefigure,theaxis,andtheplotelementwewanttoanimate6.fig=plt.figure()7.ax=plt.axes(xlim=(0,2),ylim=(-2,2))8.line,=ax.plot([],[],lw=2)9.10.#initializationfunction:plotthebackgroundofeachframe11.definit():12.line.set_data([],[])13.returnline,14.15.#animationfunction.Thisiscalledsequentially16.#note:iisframenumber17.defanimate(i):18.x=np.linspace(0,2,1000)19.y=np.sin(2*np.pi*(x-0.01*i))20.line.set_data(x,y)21.returnline,22.23.#calltheanimator.blit=Truemeansonlyre-drawthepartsthathavechanged.24.anim=animation.FuncAnimation(fig,animate,init_func=init,25.frames=200,interval=20,blit=True)26.27.#anim.save('basic_animation.mp4',fps=30,extra_args=['-vcodec','libx264'])28.29.plt.show()第四个例子:[python]viewplaincopyprint?1.#-*-coding:utf-8-*-2.3.importnumpyasnp4.importmatplotlib.pyplotasplt5.importmatplotlib.animationasanimation6.7.#每次产生一个新的坐标点8.defdata_gen():9.t=data_gen.t10.cnt=011.whilecnt1000:12.cnt+=113.t+=0.0514.yieldt,np.sin(2*np.pi*t)*np.exp(-t/10.)15.data_gen.t=016.17.#绘图18.fig,ax=plt.subplots()19.line,=ax.plot([],[],lw=2)20.ax.set_ylim(-1.1,1.1)21.ax.set_xlim(0,5)22.ax.grid()23.xdata,ydata=[],[]24.25.#因为run的参数是调用函数data_gen,所以第一个参数可以不是framenum:设置line的数据,返回line26.defrun(data):27.#updatethedata28.t,y=data29.xdata.append(t)30.ydata.append(y)31.xmin,xmax=ax.get_xlim()32.33.ift=xmax:34.ax.set_xlim(xmin,2*xmax)35.ax.figure.canvas.draw()36.line.set_data(xdata,ydata)37.38.returnline,39.40.#每隔10秒调用函数run,run的参数为函数data_gen,41.#表示图形只更新需要绘制的元素42.ani=animation.FuncAnimation(fig,run,data_gen,blit=True,interval=10,43.repeat=False)44.plt.show()再看下面的例子:[python]viewplaincopyprint?1.#-*-coding:utf-8-*-2.importnumpyasnp3.importmatplotlib.pyplotasplt4.importmatplotlib.animationasanimation5.6.#第一个参数必须为framenum7.defupdate_line(num,data,line):8.line.set_data(data[...,:num])9.returnline,10.11.fig1=plt.figure()12.13.data=np.random.rand(2,15)14.l,=plt.plot([],[],'r-')15.plt.xlim(0,1)16.plt.ylim(0,1)17.plt.xlabel('x')18.plt.title('test')19.20.#framenum从1增加大25后,返回再次从1增加到25,再返回...21.line_ani=animation.FuncAnimation(fig1,update_line,25,fargs=(data,l),interval=50,blit=True)22.23.#等同于24.#line_ani=animation.FuncAnimation(fig1,update_line,frames=25,fargs=(data,l),25.#interval=50,blit=True)26.27.#忽略frames参数,framenum会从1一直增加下去知道无穷28.#由于frame达到25以后,数据不再改变,所以你会发现到达25以后图形不再变化了29.#line_ani=animation.FuncAnimation(fig1,update_line,fargs=(data,l),30.#interval=50,blit=True)31.32.plt.show()测试如何将MatPlotLib嵌入到wxPython中测试如何将MatPlotLib嵌入到wxPython中:[python]viewplaincopyprint?1.#-*-coding:utf-8-*-2.###############################################################################3.#wxMPL_test.pyw4.#author:WuXuping5.#Date:2013-09-156.#测试如何将MatPlotLib嵌入到wxPython中7.###############################################################################8.importnumpyasnp9.importwx10.importmatplotlib11.12.#matplotlib采用WXAgg为后台,将matplotlib嵌入wxPython中13.matplotlib.use(WXAgg)14.frommatplotlib.backends.backend_wxaggimportFigureCanvasWxAggasFigureCanvas15.frommatplotlib.figureimportFigure16.frommatplotlib.backends.backend_wximportNavigationToolbar2WxasNavigationToolbar17.18.######################################################################################19.classMPL_Panel(wx.Panel):20.'''''#MPL_Panel面板,可以继承或者创建实例'''21.def__init__(self,parent):22.wx.Panel.__init__(self,parent=parent,id=-1)23.24.self.Figure=matplotlib.figure.Figure(figsize=(4,3))25.self.axes=self.Figure.add_axes([0.1,0.1,0.8,0.8])26.self.FigureCanvas=FigureCanvas(self,-1,self.Figure)27.28.#继承鼠标移动显示鼠标处坐标的事件29.self.FigureCanvas.mpl_connect('motion_notify_event',self.MPLOnMouseMove)30.31.self.NavigationToolbar=NavigationToolbar(self.FigureCanvas)32.33.self.StaticText=wx.StaticTex