国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python之matplotlib學習繪制動態更新圖實例代碼

2020-01-04 16:03:53
字體:
來源:轉載
供稿:網友

簡介

通過定時器Timer觸發事件,定時更新繪圖,可以形成動態更新圖片。下面的實例是學習《python/266105.html">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,    # 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中matplotlib,python的matplotlib

疑問

但程序運行在關閉的時候會出現應用程序錯誤,不知道什么問題。python不是有垃圾回收機制嗎,難道是內存泄露?

猜測的原因可能是在關閉的時候正在繪圖故導致應用程序出錯。通過添加Frame的析構函數,停止更新則不會出現問題。

def __del__( self ):   t.Stop() 

總結

以上就是本文關于python之matplotlib學習繪制動態更新圖實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高州市| 枣强县| 开封市| 杭州市| 兴安县| 桂林市| 安阳市| 大化| 江阴市| 丰镇市| 上栗县| 从化市| 团风县| 沙洋县| 屯门区| 哈巴河县| 京山县| 邯郸市| 南丹县| 文成县| 台东市| 益阳市| 响水县| 桃源县| 玉门市| 舒城县| 普宁市| 合作市| 久治县| 教育| 当阳市| 施甸县| 永宁县| 晴隆县| 双牌县| 栾城县| 公安县| 沾化县| 若尔盖县| 新安县| 梅河口市|