简体   繁体   English

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

[英]How can I (in Python) remove a matplotlib Artist from a plot by using blitting?

Can I remove a matplotlib artist such as a patch by using blitting? 我可以使用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)

To add a patch to a matplotlib plot by using blit you can do the following: 要使用blit将补丁添加到matplotlib图,您可以执行以下操作:

    """square is some matplotlib patch"""

    self.axes.add_patch(square)
    self.axes.draw_artist(square)
    self.canvas.blit(self.axes.bbox)

This works. 这可行。 However, can I use blit to remove this same artist from the plot? 但是,我可以使用blit从情节中删除该艺术家吗? I managed to remove it with square.remove() and I can update the plot with the self.canvas.draw() function. 我设法用square.remove()删除了它,并且可以使用self.canvas.draw()函数更新绘图。 But ofcourse this is slow and I would like to instead make use of blitting . 但是,这当然很慢,我想改用发短信

    """square is some matplotlib patch"""

    square.remove()
    self.canvas.draw()

The following does not work: 以下内容不起作用:

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

The idea to remove a blitted object would be to just blit the same region again, just not draw the object beforehands. 删除被遮挡的对象的想法是再次对同一区域进行遮挡,而不是事先绘制对象。 You may well also remove it, such that it would also not be seen if the canvas is redrawn for any other reason. 您也可以删除它,这样如果由于其他任何原因重新绘制画布也不会看到它。

It seems in the code from the question you have forgotten to call restore_region . 在代码中,您似乎忘记了要调用restore_region For a complete set of what commands are needed for blitting, see eg this question . 有关需要什么命令的完整命令集,请参阅例如this Question。

An example would be the following, where the rectangle is shown if you click the left mouse button and is removed if you hit the right button. 下面是一个示例,其中,如果您单击鼠标左键,将显示矩形;如果您单击右键,则将删除矩形。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM