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()
Scatter points are accessible through the current axis
collectionsattributeTo remove the scatters, call
.remove()on each collection in thecollectionsattribute.You will probably want to do the same for the associated texts, accessible through the current axis
textsattributeBut 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:Here is a minimal version that should work as you want :
A bunch of random clicks (left)
Followed by one right click