SoFunction
Updated on 2024-11-13

Python's matplotlib learning to draw dynamic update graph example code

synopsis

By triggering an event with the timer Timer and updating the drawing at regular intervals, it is possible to form a dynamically updated picture. The following examples are notes from studying the article "matplotlib for python developers".

realization

Implementation code and brief introduction

By = [1:] + [temp], each time the first element of the list is removed, a new element is added to its tail. This completes the dynamic update of the user data. For a more detailed explanation, see the comments section of the text.

#-*-coding:utf-8-*- 
import wx 
from  import Figure 
import matplotlib.font_manager as font_manager 
import numpy as np 
from .backend_wxagg import \ 
 FigureCanvasWxAgg as FigureCanvas 
# wxWidgets object ID for the timer 
TIMER_ID = () 
# number of data points 
POINTS = 300 
 
class PlotFigure(): 
  """Matplotlib wxFrame with animation effect""" 
  def __init__(self): 
    .__init__(self, None, wx.ID_ANY, title="CPU Usage Monitor", size=(600, 400)) 
    # Matplotlib Figure 
     = Figure((6, 4), 100) 
    # bind the Figure to the backend specific canvas 
     = FigureCanvas(self, wx.ID_ANY, ) 
    # add a subplot 
     = .add_subplot(111) 
    # limit the X and Y axes dimensions 
    .set_ylim([0, 100]) 
    .set_xlim([0, POINTS]) 
     
    .set_autoscale_on(False) 
    .set_xticks([]) 
    # we want a tick every 10 point on Y (101 is to have 10 
    .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) 
    (True) 
    # generates first "empty" plots 
     = [None] * POINTS 
    self.l_user,=(range(POINTS),,label='User %') 
 
    # add the 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 
    () 
    # save the clean background - everything but the line 
    # is drawn and saved in the pixel buffer background 
     = .copy_from_bbox() 
    # bind events coming from timer with id = TIMER_ID 
    # to the onTimer callback function 
    wx.EVT_TIMER(self, TIMER_ID, ) 
 
  def onTimer(self, evt): 
    """callback function for timer events""" 
    # restore the clean background, saved at the beginning 
    .restore_region() 
        # update the data 
    temp =(10,80) 
     = [1:] + [temp] 
    # update the plot 
    self.l_user.set_ydata() 
    # just draw the "animated" objects 
    .draw_artist(self.l_user)# It is used to efficiently update Axes data (axis ticks, labels, etc are not updated) 
    () 
if __name__ == '__main__': 
  app = () 
  frame = PlotFigure() 
  t = (frame, TIMER_ID) 
  (50) 
  () 
  () 

The results of the run are shown below:

question

But the program runs with an application error when it closes, I don't know what the problem is. doesn't python have a garbage collection mechanism, could it be a memory leak?

I guess the reason may be that the application error is caused by the fact that it is drawing when it is closed. By adding a destructor to the Frame and stopping the update, there is no problem.

def __del__( self ): 
  () 

summarize

Above is this article on python of matplotlib learning to draw dynamic update chart example code, I hope you can help. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!