
通过定时器Timer触发事件,定时更新绘图,可以形成动态更新图片。下面的实例是学习《matplotlib for python developers》一文的笔记。
实现代码及简单介绍
通过self.user = self.user[1:] + [temp],每次删除列表的第一元素,在其尾部添加新的元素。这样完成user数据的动态更新。其他详细的解释见文中的注释部分。
#-*-Coding:utf-8-*- import wx from matplotlib.figure import figure import matplotlib.Font_manager as Font_manager import numpy as np from matplotlib.backends.backend_wxagg import \ figureCanvasWxAgg as figureCanvas # wxWidgets object ID for the timer TIMER_ID = wx.NewID() # number of data points POINTS = 300 class Plotfigure(wx.Frame): """Matplotlib wxFrame with animation effect""" def @R_404_4263@(self): wx.Frame.@R_404_4263@(self,None,wx.ID_ANY,title="cpu Usage Monitor",size=(600,400)) # Matplotlib figure self.fig = figure((6,4),100) # bind the figure to the backend specific canvas self.canvas = figureCanvas(self,self.fig) # add a subplot self.ax = self.fig.add_subplot(111) # limit the X and Y axes dimensions self.ax.set_ylim([0,100]) self.ax.set_xlim([0,POINTS]) self.ax.set_autoscale_on(False) self.ax.set_xticks([]) # we want a tick every 10 point on Y (101 is to have 10 self.ax.set_yticks(range(0,101,10)) # disable autoscale,since we don't want the Axes to ad # draw a grID (it will be only for Y) self.ax.grID(True) # generates first "empty" plots self.user = [None] * POINTS self.l_user,=self.ax.plot(range(POINTS),self.user,label='User %') # add the legend self.ax.legend(loc='upper center',ncol=4,prop=Font_manager.FontPropertIEs(size=10)) # force a draw on the canvas() # trick to show the grID and the legend self.canvas.draw() # save the clean background - everything but the line # is drawn and saved in the pixel buffer background self.bg = self.canvas.copy_from_bBox(self.ax.bBox) # bind events coming from timer with ID = TIMER_ID # to the onTimer callback function wx.EVT_TIMER(self,TIMER_ID,self.onTimer) def onTimer(self,evt): """callback function for timer events""" # restore the clean background,saved at the beginning self.canvas.restore_region(self.bg) # update the data temp =np.random.randint(10,80) self.user = self.user[1:] + [temp] # update the plot self.l_user.set_ydata(self.user) # just draw the "animated" objects self.ax.draw_artist(self.l_user)# It is used to efficIEntly update Axes data (axis ticks,labels,etc are not updated) self.canvas.blit(self.ax.bBox) if __name__ == '__main__': app = wx.PySimpleApp() frame = Plotfigure() t = wx.Timer(frame,TIMER_ID) t.Start(50) frame.Show() app.MainLoop()
运行结果如下所示:
疑问但程序运行在关闭的时候会出现应用程序错误,不知道什么问题。python不是有垃圾回收机制吗,难道是内存泄露?
猜测的原因可能是在关闭的时候正在绘图故导致应用程序出错。通过添加Frame的析构函数,停止更新则不会出现问题。
def __del__( self ): t.Stop()总结
以上就是本文关于python之matplotlib学习绘制动态更新图实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
您可能感兴趣的文章:Python+matplotlib实现华丽的文本框演示代码python+matplotlib实现动态绘制图片实例代码(交互式绘图)python+matplotlib绘制饼图散点图实例代码Python+matplotlib绘制不同大小和颜色散点图实例Python实现在tkinter中使用matplotlib绘制图形的方法示例Python使用matplotlib实现绘制自定义图形功能示例python+matplotlib绘制3D条形图实例代码Python使用matplotlib填充图形指定区域代码示例 总结以上是内存溢出为你收集整理的python之matplotlib学习绘制动态更新图实例代码全部内容,希望文章能够帮你解决python之matplotlib学习绘制动态更新图实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)