python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼
簡(jiǎn)介
通過(guò)定時(shí)器Timer觸發(fā)事件,定時(shí)更新繪圖,可以形成動(dòng)態(tài)更新圖片。下面的實(shí)例是學(xué)習(xí)《matplotlib for python developers》一文的筆記。
實(shí)現(xiàn)
實(shí)現(xiàn)代碼及簡(jiǎn)單介紹
通過(guò)self.user = self.user[1:] + [temp],每次刪除列表的第一元素,在其尾部添加新的元素。這樣完成user數(shù)據(jù)的動(dòng)態(tài)更新。其他詳細(xì)的解釋見文中的注釋部分。
#-*-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()
運(yùn)行結(jié)果如下所示:

疑問(wèn)
但程序運(yùn)行在關(guān)閉的時(shí)候會(huì)出現(xiàn)應(yīng)用程序錯(cuò)誤,不知道什么問(wèn)題。python不是有垃圾回收機(jī)制嗎,難道是內(nèi)存泄露?
猜測(cè)的原因可能是在關(guān)閉的時(shí)候正在繪圖故導(dǎo)致應(yīng)用程序出錯(cuò)。通過(guò)添加Frame的析構(gòu)函數(shù),停止更新則不會(huì)出現(xiàn)問(wèn)題。
def __del__( self ): t.Stop()
總結(jié)
以上就是本文關(guān)于python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
python學(xué)生管理系統(tǒng)學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了python學(xué)生管理系統(tǒng)的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Django--權(quán)限Permissions的例子
今天小編就為大家分享一篇Django--權(quán)限Permissions的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
nlp計(jì)數(shù)法應(yīng)用于PTB數(shù)據(jù)集示例詳解
這篇文章主要為大家介紹了nlp計(jì)數(shù)法應(yīng)用于PTB數(shù)據(jù)集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
關(guān)于Python正則表達(dá)式 findall函數(shù)問(wèn)題詳解
在寫正則表達(dá)式的時(shí)候總會(huì)遇到不少的問(wèn)題,本文講述了Python正則表達(dá)式中 findall()函數(shù)和多個(gè)表達(dá)式元組相遇的時(shí)候會(huì)出現(xiàn)的問(wèn)題2018-03-03
Python lambda和Python def區(qū)別分析
Python支持一種有趣的語(yǔ)法,它允許你快速定義單行的最小函數(shù)。這些叫做lambda的函數(shù),是從Lisp借用來(lái)的,可以用在任何需要函數(shù)的地方2014-11-11
Python Pandas 獲取列匹配特定值的行的索引問(wèn)題
這篇文章主要介紹了Python Pandas 獲取列匹配特定值的行的索引問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Python繪圖系統(tǒng)之自定義一個(gè)坐標(biāo)列表控件
這篇文章主要為大家詳細(xì)介紹了Python如何編寫一個(gè)繪圖系統(tǒng),可以實(shí)現(xiàn)自定義一個(gè)坐標(biāo)列表控件,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-08-08

