how to remove previous matplotlib scatter with mouse clicking

258 views Asked by At

I want make just one new scatter and delete previous scatter on the fig But i can't the method to remove previous scatter,so the number of scatter is increased when i click mouse i make to remain one scatter when i click the left button of mouse here is my code

with clicking on the left mouse button , i can make scatter of mouse coordinate and record all click with clicking on the right mouse button, i can remove all scatter

i just remain one scatter with lately the left button of mouse when i click on the left mouse button please help me

here is my code

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 8))
plt.grid(True)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Center Pin Coordinate')
plt.text(825, 750, 'left button: mark')
plt.text(825, 700, 'right button: import')

ax.set(xlim=[0, 925], ylim=[0, 750])
ax.set_aspect('auto', adjustable='box')

def add_point(event):
    if event.inaxes != ax:
        return

    if event.button ==1:
       
        xdata = event.xdata
        ydata = event.ydata
        xdata = round(xdata,1)
        ydata = round(ydata,1)
        plt.scatter(xdata, ydata, c='red', edgecolors='black', s=200)
        plt.text(xdata, ydata, (xdata, ydata))
            
        plt.show()

    if event.button == 3:
        xdata = []
        ydata = []
        # plt.scatter(xdata, ydata)
        plt.cla()
        plt.grid(True)
        plt.xlabel('x')
        plt.ylabel('y')
        plt.title('Center Pin Coordinate')
        plt.text(825, 750, 'left button: mark')
        plt.text(825, 700, 'right button: import')
        ax.set(xlim=[0, 925], ylim=[0, 750])
        ax.set_aspect('auto', adjustable='box')
        plt.show()

cid = plt.connect('button_press_event', add_point)
plt.show()
1

There are 1 answers

0
thmslmr On

Scatter points are accessible through the current axis collections attribute

all_scatters = plt.gca().collections

To remove the scatters, call .remove() on each collection in the collections attribute.

for scatter in plt.gca().collections:
    scatter.remove()

You will probably want to do the same for the associated texts, accessible through the current axis texts attribute

for text in plt.gca().texts:
    text.remove()

But be careful, this will also delete the 2 initial texts you put on the axis ("left button: mark" and "right button: import").
If you really want to keep the figure as it is, you can escape the remove() call for those 2 specific texts with:

for text in plt.gca().texts:
    value = text.get_text()
    if value not in ["left button: mark", "right button: import"]:
        text.remove()

Here is a minimal version that should work as you want :

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 8))
plt.grid(True)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Center Pin Coordinate')
plt.text(825, 750, 'left button: mark')
plt.text(825, 700, 'right button: import')

ax.set(xlim=[0, 925], ylim=[0, 750])
ax.set_aspect('auto', adjustable='box')

def add_point(event):
    if event.inaxes != ax:
        return
    
    if event.button == 1:
        xdata = round(event.xdata, 1)
        ydata = round(event.ydata, 1)
        
        plt.scatter(xdata, ydata, c='red', edgecolors='black', s=200)
        plt.text(xdata, ydata, (xdata, ydata))

    if event.button == 3:
        
        for scatter in plt.gca().collections:
            scatter.remove()
            
        for text in plt.gca().texts:
            value = text.get_text()
            if value not in ["left button: mark", "right button: import"]:
                text.remove()

cid = plt.connect('button_press_event', add_point)
plt.show()

A bunch of random clicks (left)

enter image description here

Followed by one right click

enter image description here