注意
跳至末尾以下載完整範例程式碼。
使用 Blitting 加快渲染速度#
Blitting 是光柵圖形中的一種標準技術,在 Matplotlib 的環境中,可用於(大幅)提高互動式圖表的效能。例如,animation
和 widgets
模組在內部使用 blitting。在此,我們示範如何在這些類別之外實作您自己的 blitting。
Blitting 透過將所有不變的圖形元素一次渲染到背景圖像中來加速重複繪製。然後,每次繪製時,只需要將變化的元素繪製到此背景上。例如,如果軸的限制沒有改變,我們可以一次渲染空的軸,包括所有刻度和標籤,然後僅繪製稍後變化的資料。
策略是
準備恆定的背景
繪製圖表,但排除所有您想要製作動畫的藝術家,方法是將它們標記為動畫 (請參閱
Artist.set_animated
)。儲存 RBGA 緩衝區的副本。
渲染個別圖像
還原 RGBA 緩衝區的副本。
使用
Axes.draw_artist
/Figure.draw_artist
重新繪製動畫藝術家。在螢幕上顯示產生的圖像。
此程序的結果之一是您的動畫藝術家總是繪製在靜態藝術家的頂部。
並非所有後端都支援 blitting。您可以透過 FigureCanvasBase.supports_blit
屬性檢查給定的畫布是否支援。
警告
此程式碼不適用於 macosx 後端(但適用於 Mac 上的其他 GUI 後端)。
最小範例#
我們可以將 FigureCanvasAgg
方法 copy_from_bbox
和 restore_region
與在我們的藝術家上設定 animated=True
結合使用,來實作使用 blitting 加速渲染的最小範例
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots()
# animated=True tells matplotlib to only draw the artist when we
# explicitly request it
(ln,) = ax.plot(x, np.sin(x), animated=True)
# make sure the window is raised, but the script keeps going
plt.show(block=False)
# stop to admire our empty window axes and ensure it is rendered at
# least once.
#
# We need to fully draw the figure at its final size on the screen
# before we continue on so that :
# a) we have the correctly sized and drawn background to grab
# b) we have a cached renderer so that ``ax.draw_artist`` works
# so we spin the event loop to let the backend process any pending operations
plt.pause(0.1)
# get copy of entire figure (everything inside fig.bbox) sans animated artist
bg = fig.canvas.copy_from_bbox(fig.bbox)
# draw the animated artist, this uses a cached renderer
ax.draw_artist(ln)
# show the result to the screen, this pushes the updated RGBA buffer from the
# renderer to the GUI framework so you can see it
fig.canvas.blit(fig.bbox)
for j in range(100):
# reset the background back in the canvas state, screen unchanged
fig.canvas.restore_region(bg)
# update the artist, neither the canvas state nor the screen have changed
ln.set_ydata(np.sin(x + (j / 100) * np.pi))
# re-render the artist, updating the canvas state, but not the screen
ax.draw_artist(ln)
# copy the image to the GUI state, but screen might not be changed yet
fig.canvas.blit(fig.bbox)
# flush any pending GUI events, re-painting the screen if needed
fig.canvas.flush_events()
# you can put a pause in if you want to slow things down
# plt.pause(.1)

此範例可以運作並顯示簡單的動畫,但因為我們只抓取一次背景,如果圖表的像素大小發生變化(因為圖表的大小或 dpi 發生變化),背景將會無效,並導致不正確(但有時看起來很酷!)的圖像。還有一個全域變數和相當多的樣板程式碼,這表示我們應該將其包裝在一個類別中。
基於類別的範例#
我們可以利用類別來封裝樣板邏輯和還原背景、繪製藝術家,然後將結果 blit 到螢幕的狀態。此外,我們可以使用 'draw_event'
回呼,以便在完整重新繪製發生時擷取新的背景,以正確處理調整大小。
class BlitManager:
def __init__(self, canvas, animated_artists=()):
"""
Parameters
----------
canvas : FigureCanvasAgg
The canvas to work with, this only works for subclasses of the Agg
canvas which have the `~FigureCanvasAgg.copy_from_bbox` and
`~FigureCanvasAgg.restore_region` methods.
animated_artists : Iterable[Artist]
List of the artists to manage
"""
self.canvas = canvas
self._bg = None
self._artists = []
for a in animated_artists:
self.add_artist(a)
# grab the background on every draw
self.cid = canvas.mpl_connect("draw_event", self.on_draw)
def on_draw(self, event):
"""Callback to register with 'draw_event'."""
cv = self.canvas
if event is not None:
if event.canvas != cv:
raise RuntimeError
self._bg = cv.copy_from_bbox(cv.figure.bbox)
self._draw_animated()
def add_artist(self, art):
"""
Add an artist to be managed.
Parameters
----------
art : Artist
The artist to be added. Will be set to 'animated' (just
to be safe). *art* must be in the figure associated with
the canvas this class is managing.
"""
if art.figure != self.canvas.figure:
raise RuntimeError
art.set_animated(True)
self._artists.append(art)
def _draw_animated(self):
"""Draw all of the animated artists."""
fig = self.canvas.figure
for a in self._artists:
fig.draw_artist(a)
def update(self):
"""Update the screen with animated artists."""
cv = self.canvas
fig = cv.figure
# paranoia in case we missed the draw event,
if self._bg is None:
self.on_draw(None)
else:
# restore the background
cv.restore_region(self._bg)
# draw all of the animated artists
self._draw_animated()
# update the GUI state
cv.blit(fig.bbox)
# let the GUI event loop process anything it has to do
cv.flush_events()
以下是我們如何使用我們的類別。這是一個比第一種情況稍微複雜的範例,因為我們還新增了一個文字影格計數器。
# make a new figure
fig, ax = plt.subplots()
# add a line
(ln,) = ax.plot(x, np.sin(x), animated=True)
# add a frame number
fr_number = ax.annotate(
"0",
(0, 1),
xycoords="axes fraction",
xytext=(10, -10),
textcoords="offset points",
ha="left",
va="top",
animated=True,
)
bm = BlitManager(fig.canvas, [ln, fr_number])
# make sure our window is on the screen and drawn
plt.show(block=False)
plt.pause(.1)
for j in range(100):
# update the artists
ln.set_ydata(np.sin(x + (j / 100) * np.pi))
fr_number.set_text(f"frame: {j}")
# tell the blitting manager to do its thing
bm.update()

此類別不依賴 pyplot
,且適合嵌入到較大的 GUI 應用程式中。
腳本總執行時間:(0 分鐘 1.082 秒)