我可以使用blitting删除matplotlib艺术家,例如补丁吗?
isContinue
要使用 blit 将补丁添加到matplotlib图中,您可以执行以下操作:
"""Some background code:"""
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
这很有效。但是,我可以使用 blit 从剧情中删除同一位艺术家吗?
我设法用 """square is some matplotlib patch"""
self.axes.add_patch(square)
self.axes.draw_artist(square)
self.canvas.blit(self.axes.bbox)
将其删除,然后我可以使用square.remove()
函数更新图表。但是当然这很慢,我想反而使用 blitting 。
self.canvas.draw()
以下不起作用:
"""square is some matplotlib patch"""
square.remove()
self.canvas.draw()
答案 0 :(得分:1)
删除blitted对象的想法是再次对同一区域进行blit,而不是在prehands之前绘制对象。您也可以将其删除,以便在出于任何其他原因重绘画布时也不会看到它。
在您忘记致电restore_region
的问题的代码中。有关blitting所需的完整命令,请参阅例如this question。
下面是一个示例,如果单击鼠标左键则会显示矩形,如果单击右键则会删除矩形。
import matplotlib.pyplot as plt
import numpy as np
class Test:
def __init__(self):
self.fig, self.ax = plt.subplots()
# Axis with large plot
self.ax.imshow(np.random.random((5000,5000)))
# Draw the canvas once
self.fig.canvas.draw()
# Store the background for later
self.background = self.fig.canvas.copy_from_bbox(self.ax.bbox)
# create square
self.square = plt.Rectangle([2000,2000],900,900, zorder=3, color="crimson")
# Create callback to mouse movement
self.cid = self.fig.canvas.callbacks.connect('button_press_event',
self.callback)
plt.show()
def callback(self, event):
if event.inaxes == self.ax:
if event.button == 1:
# Update point's location
self.square.set_xy((event.xdata-450, event.ydata-450))
# Restore the background
self.fig.canvas.restore_region(self.background)
# draw the square on the screen
self.ax.add_patch(self.square)
self.ax.draw_artist(self.square)
# blit the axes
self.fig.canvas.blit(self.ax.bbox)
else:
self.square.remove()
self.fig.canvas.restore_region(self.background)
self.fig.canvas.blit(self.ax.bbox)
tt = Test()