user24559516
user24559516

Reputation: 13

Remove matplotlib Poly Editor polygon from plot

I am using the Poly Editor from Matplotlib Link to create rectangles in an image. The image is loaded in a seperate class using following function:

def load_image(self):
    canvas = self.gui.dataPlot  # Reference to matplotlibPlot widget
    fig = plt.figure(1,canvas=canvas, figsize = (1.4,1.7))
    self.ax = fig.add_subplot(111)
    self.image = cv2.cvtColor(cv2.imread(self.images[self.c_im]), cv2.COLOR_BGR2RGB)
    self.ax.clear()
    im = self.ax.imshow(self.image)
    plt.draw()

The polygon objects are created in the same class using an UI button:

def create_rectangle(self, label):
    xs = np.array([500, 1000, 1000,  500])
    ys = np.array([500,   500, 1000, 1000])
    poly = Polygon(np.column_stack([xs, ys]), animated=True, fill=True,closed=True, facecolor='white', alpha=0.1)
    self.ax.add_patch(poly)
    p = PolygonInteractor(self.ax, poly)
    plt.show()

When the image changes, I want to remove all polygon objects to be able to create new ones. So far I have tried those two options to remove them:

self.ax.clear()
[p.remove() for p in reversed(self.ax.patches)]

However, when I use them, the polygon object is still visible. It seems like the polygon object is not a member of self.ax.patches. Someone has a suggestion on how I can remove them?

Upvotes: 0

Views: 27

Answers (0)

Related Questions