Open In App

Matplotlib.figure.Figure.draw_artist() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements.

matplotlib.figure.Figure.draw_artist() function

The draw_artist() method of figure module of matplotlib library is used to draw matplotlib.artist.Artist instance a only.

Syntax: draw_artist(self, a)

Parameters: This accept the following parameters that are described below:

  • a: This parameter is the artist.

Returns: This method does not return any value.

Below examples illustrate the matplotlib.figure.Figure.draw_artist() function in matplotlib.figure:

Example 1:




# Implementation of matplotlib function 
from random import randint, choice
import time
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
   
  
back_color = "black"
colors = ['red', 'green', 'blue', 'purple']
width, height = 4, 4
   
fig, ax = plt.subplots()
ax.set(xlim =[0, width], ylim =[0, height])
   
fig.canvas.draw()
   
def update():
    x = randint(0, width - 1)
    y = randint(0, height - 1)
   
    arti = mpatches.Rectangle(
        (x, y), 1, 1,
        facecolor = choice(colors),
        edgecolor = back_color
    )
    ax.add_artist(arti)
   
    start = time.time()
    fig.draw_artist(arti)
    fig.canvas.blit(ax.bbox)
    print("Draw at time :", time.time() - start)
   
timer = fig.canvas.new_timer(interval = 1)
timer.add_callback(update)
timer.start()
  
fig.suptitle('matplotlib.figure.Figure.draw_artist() \
function Example') 
  
plt.show()


Output:

Draw at time : 0.2968637943267822
Draw at time : 0.031249523162841797
Draw at time : 0.015642404556274414
Draw at time : 0.015624523162841797
Draw at time : 0.015607357025146484
Draw at time : 0.015637636184692383
....
...
so on.

Example 2:




# Implementation of matplotlib function 
import matplotlib.pyplot as plt
import numpy as np
import time
   
fig, ax = plt.subplots()
line, = ax.plot(np.random.randn(100))
   
tstart = time.time()
num_plots = 0
fig.canvas.draw()
  
while time.time()-tstart < 5:
    line.set_ydata(np.random.randn(100))
    fig.draw_artist(ax.patch)
    fig.draw_artist(line)
    num_plots += 1
      
fig.suptitle('matplotlib.figure.Figure.draw_artist()\
 function Example') 
  
plt.show()


Output:



Last Updated : 30 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads