Python matplotlib learning to draw dynamic update graph instance code, pythonmatplotlib

Source: Internet
Author: User

Python matplotlib learning to draw dynamic update graph instance code, pythonmatplotlib
Introduction

The Timer triggers an event and regularly updates the drawing to form a dynamic update image. The following example shows the notes for learning matplotlib for python developers.

Implementation

Implementation Code and brief introduction

Use self. user = self. user [1:] + [temp] to delete the first element of the list and add a new element at the end of the list. In this way, the user data is dynamically updated. For more detailed explanations, see the comments section.

#-*-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() 

The running result is as follows:

Question

However, an application error occurs when the program is running. Isn't python a garbage collection mechanism? Is it a memory leak?

The cause of speculation may be that an application error occurs because the drawing is being performed when the program is closed. By adding the destructor of the Frame, stop updating.

def __del__( self ):   t.Stop() 
Summary

The above is all about the python matplotlib learning to draw dynamic update graph instance code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.