Python中的 Matplotlib.figure.Figure.draw_artist()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.draw_artist()函数
matplotlib库的figure模块的draw_artist()方法只用于绘制matplotlib.artist.Artist实例a。
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.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.draw_artist()函数:
示例 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()
输出:
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.
示例 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()
输出: