网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python中matplotlib绘制动态更新图的方法-创新互联

这篇文章主要介绍python中matplotlib绘制动态更新图的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联是专业的温州网站建设公司,温州接单;提供成都网站建设、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行温州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

简介

通过定时器Timer触发事件,定时更新绘图,可以形成动态更新图片。

实现

实现代码及简单介绍

通过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是什么意思

Python是一种跨平台的、具有解释性、编译性、互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。

以上是“python中matplotlib绘制动态更新图的方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:python中matplotlib绘制动态更新图的方法-创新互联
浏览路径:http://bjjierui.cn/article/ccophj.html

其他资讯