加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python之matplotlib学习(三) 画动态更新图【转】

(2013-10-31 17:56:42)
分类: python

通过定时器Timer触发事件,定时更新绘图,可以形成动态更新图片。下面的实例是学习《matplotlib for python developers》一文的笔记。

实例如下:

通过self.user = self.user[1:] + [temp],每次删除列表的第一元素,在其尾部添加新的元素。这样完成user数据的动态更新。其他详细的解释见文中的注释部分。

[python] view plaincopy
  1. #-*-coding:utf-8-*-  
  2. import wx  
  3. from matplotlib.figure import Figure  
  4. import matplotlib.font_manager as font_manager  
  5. import numpy as np  
  6. from matplotlib.backends.backend_wxagg import  
  7.   FigureCanvasWxAgg as FigureCanvas  
  8. wxWidgets object ID for the timer  
  9. TIMER_ID wx.NewId()  
  10. number of data points  
  11. POINTS 300  
  12.   
  13. class PlotFigure(wx.Frame):  
  14.     """Matplotlib wxFrame with animation effect"""  
  15.     def __init__(self):  
  16.         wx.Frame.__init__(selfNonewx.ID_ANY, title="CPU Usage Monitor"size=(600400))  
  17.         Matplotlib Figure  
  18.         self.fig Figure((64), 100 
  19.         bind the Figure to the backend specific canvas  
  20.         self.canvas FigureCanvas(selfwx.ID_ANY, self.fig)  
  21.         add subplot  
  22.         self.ax self.fig.add_subplot(111 
  23.         limit the and axes dimensions  
  24.         self.ax.set_ylim([0100])  
  25.         self.ax.set_xlim([0POINTS])  
  26.          
  27.         self.ax.set_autoscale_on(False 
  28.         self.ax.set_xticks([])  
  29.         we want tick every 10 point on (101 is to have 10  
  30.         self.ax.set_yticks(range(010110))  
  31.         disable autoscale, since we don't want the Axes to ad  
  32.         draw grid (it will be only for Y)  
  33.         self.ax.grid(True 
  34.         generates first "empty" plots  
  35.         self.user [NonePOINTS  
  36.         self.l_user,=self.ax.plot(range(POINTS),self.user,label='User %' 
  37.   
  38.         add the legend  
  39.         self.ax.legend(loc='upper center' 
  40.                            ncol=4 
  41.                            prop=font_manager.FontProperties(size=10))  
  42.         force draw on the canvas()  
  43.         trick to show the grid and the legend  
  44.         self.canvas.draw()  
  45.         save the clean background everything but the line  
  46.         is drawn and saved in the pixel buffer background  
  47.         self.bg self.canvas.copy_from_bbox(self.ax.bbox)  
  48.         bind events coming from timer with id TIMER_ID  
  49.         to the onTimer callback function  
  50.         wx.EVT_TIMER(selfTIMER_ID, self.onTimer)  
  51.   
  52.     def onTimer(selfevt):  
  53.         """callback function for timer events"""  
  54.         restore the clean background, saved at the beginning  
  55.         self.canvas.restore_region(self.bg)  
  56.                 update the data  
  57.         temp =np.random.randint(10,80 
  58.         self.user self.user[1:] [temp]  
  59.         update the plot  
  60.         self.l_user.set_ydata(self.user)  
  61.         just draw the "animated" objects  
  62.         self.ax.draw_artist(self.l_user)It is used to efficiently update Axes data (axis ticks, labels, etc are not updated)  
  63.         self.canvas.blit(self.ax.bbox)  
  64. if __name__ == '__main__' 
  65.     app wx.PySimpleApp()  
  66.     frame PlotFigure()  
  67.     wx.Timer(frame, TIMER_ID)  
  68.     t.Start(50 
  69.     frame.Show()  
  70.     app.MainLoop()  

运行结果如下所示:

http://hi.csdn.net/attachment/201110/9/0_1318168012Asz0.gif画动态更新图【转】" />

但程序运行在关闭的时候会出现应用程序错误,不知道什么问题。python不是有垃圾回收机制吗,难道是内存泄露?

猜测的原因可能是在关闭的时候正在绘图故导致应用程序出错。通过添加Frame的析构函数,停止更新则不会出现问题。

 

[python] view plaincopy
  1. def __del__( self ):  
  2.     t.Stop()  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有