I am trying to write a script to interactively plot a scatter plot using matplotlib. It is important to me to delete some points by mouse click event or delete button on the keyboard. My goal is to clean the plot from the undesired points and generate a new dataframe with the clean points. I spent the whole day trying to figure it out and could write this script.
I would appreciate any suggestions.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 5, 6, 5]
def onpick(event):
this_artist = event.artist
print(this_artist)
plt.gca().picked_object = this_artist
def on_key(event):
if event.key == u'delete':
ax = plt.gca()
if ax.picked_object:
ax.picked_object.remove()
ax.picked_object = None
ax.figure.canvas.draw()
fig, ax = plt.subplots()
ax= plt.scatter(x,y)
fig.canvas.mpl_connect('pick_event', onpick)
cid = fig.canvas.mpl_connect('key_press_event', on_key)
plt.show()