如何(在Python中)使用blitting从绘图中删除matplotlib艺术家?

如何(在Python中)使用blitting从绘图中删除matplotlib艺术家?,python,matplotlib,wxpython,blit,matplotlib-widget,Python,Matplotlib,Wxpython,Blit,Matplotlib Widget,我可以使用blitting删除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.a

我可以使用blitting删除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将修补程序添加到matplotlib绘图,可以执行以下操作:

    """square is some matplotlib patch"""

    self.axes.add_patch(square)
    self.axes.draw_artist(square)
    self.canvas.blit(self.axes.bbox)
这很有效。但是,我可以使用blit将同一位艺术家从情节中删除吗? 我用
square.remove()
删除了它,我可以用
self.canvas.draw()函数更新绘图。但这当然是缓慢的,我想改为使用bliting

    """square is some matplotlib patch"""

    square.remove()
    self.canvas.draw()
以下操作不起作用:

    square.remove()
    self.canvas.blit(self.axes.bbox)

删除blit对象的想法是再次blit相同的区域,而不是在之前绘制对象。您也可以删除它,这样,如果画布由于任何其他原因被重新绘制,也不会看到它

在问题的代码中,您似乎忘记调用
restore\u region
。有关blitting所需命令的完整集合,请参见例如

下面是一个示例,单击鼠标左键时显示矩形,单击鼠标右键时删除矩形

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()

我不确定我是否正确理解了这个问题,但是为什么不在没有正方形的情况下再次blit画布呢?
square.remove()
,然后
self.canvas.blit(self.axes.bbox)
似乎不起作用。这位艺术家一直都在现场,谢谢!只是在没有正方形作品的情况下重新摆放画布。