python之matplotlib学习绘制动态更新图实例代码


Posted in Python onJanuary 23, 2018

简介

通过定时器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 __init__(self): 
    wx.Frame.__init__(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, wx.ID_ANY, 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之matplotlib学习绘制动态更新图实例代码

疑问

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

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

def __del__( self ): 
  t.Stop()

总结

以上就是本文关于python之matplotlib学习绘制动态更新图实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
Python lambda和Python def区别分析
Nov 30 Python
Python脚本实现虾米网签到功能
Apr 12 Python
简单讲解Python编程中namedtuple类的用法
Jun 21 Python
python正则实现提取电话功能
Feb 24 Python
Python 爬虫之Beautiful Soup模块使用指南
Jul 05 Python
Python实现带参数的用户验证功能装饰器示例
Dec 14 Python
Python使用itchat 功能分析微信好友性别和位置
Aug 05 Python
Numpy与Pytorch 矩阵操作方式
Dec 27 Python
Python3 Click模块的使用方法详解
Feb 12 Python
python实现ftp文件传输功能
Mar 20 Python
Django权限控制的使用
Jan 07 Python
如何利用Python实现一个论文降重工具
Jul 09 Python
彻底搞懂Python字符编码
Jan 23 #Python
Python实现PS滤镜的万花筒效果示例
Jan 23 #Python
python处理csv数据动态显示曲线实例代码
Jan 23 #Python
Python+matplotlib实现华丽的文本框演示代码
Jan 22 #Python
CentOS7.3编译安装Python3.6.2的方法
Jan 22 #Python
Python OpenCV实现图片上输出中文
Jan 22 #Python
python批量替换页眉页脚实例代码
Jan 22 #Python
You might like
php使用glob函数快速查询指定目录文件的方法
2014/11/15 PHP
php计划任务之ignore_user_abort函数实现方法
2015/01/08 PHP
PHP随机生成信用卡卡号的方法
2015/03/23 PHP
thinkPHP多语言切换设置方法详解
2016/11/11 PHP
PHP设计模式之迭代器模式Iterator实例分析【对象行为型】
2020/04/26 PHP
javascript jQuery插件练习
2008/12/24 Javascript
Jquery 插件学习实例1 插件制作说明与tableUI优化
2010/04/02 Javascript
javascript实现回车键提交表单方法总结
2015/01/10 Javascript
jquery+CSS3模拟Path2.0动画菜单效果代码
2015/08/31 Javascript
js基于setTimeout与setInterval实现多线程
2016/06/17 Javascript
原生JS实现的放大镜效果实例代码
2016/10/15 Javascript
vue.js的提示组件
2017/03/02 Javascript
如何自动化部署项目?折腾服务器之旅~
2019/04/16 Javascript
Node.js系列之安装配置与基本使用(1)
2019/08/30 Javascript
ng-alain的sf如何自定义部件的流程
2020/06/12 Javascript
[52:52]完美世界DOTA2联赛PWL S3 LBZS vs access 第一场 12.10
2020/12/13 DOTA
Python高效编程技巧
2013/01/07 Python
python3生成随机数实例
2014/10/20 Python
Python创建文件和追加文件内容实例
2014/10/21 Python
玩转python爬虫之爬取糗事百科段子
2016/02/17 Python
Python实现统计给定列表中指定数字出现次数的方法
2018/04/11 Python
python中logging模块的一些简单用法的使用
2019/02/22 Python
django搭建项目配置环境和创建表过程详解
2019/07/22 Python
python实现xlwt xlrd 指定条件给excel行添加颜色
2020/07/14 Python
python 对xml解析的示例
2021/02/27 Python
探讨HTML5移动开发的几大特性(必看)
2015/12/30 HTML / CSS
英国著名的药妆网站:Escentual
2016/07/29 全球购物
英国儿童鞋和靴子:Start-Rite
2019/05/06 全球购物
Lookfantastic意大利官网:英国知名美妆购物网站
2019/05/31 全球购物
作文评语大全
2014/04/23 职场文书
会计实训报告范文
2014/11/04 职场文书
优秀班组申报材料
2014/12/25 职场文书
Python读取文件夹下的所有文件实例代码
2021/04/02 Python
MySQL 如何分析查询性能
2021/05/12 MySQL
Python import模块的缓存问题解决方案
2021/06/02 Python
教你用Java在个人电脑上实现微信扫码支付
2021/06/13 Java/Android