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

 更新时间:2018年01月23日 09:12:22   作者:bigxu  
这篇文章主要介绍了python之matplotlib学习绘制动态更新图实例代码,文中涉及具体实现代码,演示效果及运行时出现的问题分析等相关内容,小编觉得还是挺不错的,这里分享给大家,需要的朋友可以参考下

简介

通过定时器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不是有垃圾回收机制吗,难道是内存泄露?

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

def __del__( self ): 
  t.Stop() 

总结

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

相关文章

  • Python绘图实现台风路径可视化代码实例

    Python绘图实现台风路径可视化代码实例

    这篇文章主要介绍了Python绘图实现台风路径可视化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Python编程中对super函数的正确理解和用法解析

    Python编程中对super函数的正确理解和用法解析

    可能有人会想到,Python中既然可以直接通过父类名调用父类方法为什么还会存在super函数?其实,很多人对Python中的super函数的认识存在误区,本文我们就带来在Python编程中对super函数的正确理解和用法解析
    2016-07-07
  • 我用Python抓取了7000 多本电子书案例详解

    我用Python抓取了7000 多本电子书案例详解

    这篇文章主要介绍了我用Python抓取了7000 多本电子书案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Python爬虫爬取微博热搜保存为 Markdown 文件的源码

    Python爬虫爬取微博热搜保存为 Markdown 文件的源码

    这篇文章主要介绍了Python爬虫爬取微博热搜保存为 Markdown 文件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Python实现简单http服务器

    Python实现简单http服务器

    这篇文章主要为大家详细介绍了Python实现一个简单http服务器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Python3实现连接SQLite数据库的方法

    Python3实现连接SQLite数据库的方法

    这篇文章主要介绍了Python3实现连接SQLite数据库的方法,在Python数据库编程中有着广泛的应用,需要的朋友可以参考下
    2014-08-08
  • 详解Python 使用 selenium 进行自动化测试或者协助日常工作

    详解Python 使用 selenium 进行自动化测试或者协助日常工作

    这篇文章主要介绍了Python 使用 selenium 进行自动化测试 或者协助日常工作,我们可以使用 selenium 来帮助我们进行自动化的 Web 测试,也可以通过 selenium 操作浏览器做一些重复的,简单的事情,来减轻我们的工作
    2021-09-09
  • Python实现汇率转换操作

    Python实现汇率转换操作

    这篇文章主要介绍了Python实现汇率转换操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • pytorch的Backward过程用时太长问题及解决

    pytorch的Backward过程用时太长问题及解决

    这篇文章主要介绍了pytorch的Backward过程用时太长问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • python的mysqldb安装步骤详解

    python的mysqldb安装步骤详解

    这篇文章主要介绍了python的mysqldb安装步骤详解的相关资料,这里提供实现的具体步骤,需要的朋友可以参考下
    2017-08-08

最新评论