更新时间:2023-02-06 07:41:03
首先,您应该好好阅读 此处的事件文档.
Firstly, you should have a good read of the events docs here.
您可以附加一个在单击鼠标时调用的函数.如果你维护一个可以被挑选的艺术家列表(在这种情况下是点),那么你可以询问鼠标点击事件是否在艺术家内部,并调用艺术家的 remove
方法.如果没有,您可以创建一个新的艺术家,并将其添加到可点击的点列表中:
You can attach a function which gets called whenever the mouse is clicked. If you maintain a list of artists (points in this case) which can be picked, then you can ask if the mouse click event was inside the artists, and call the artist's remove
method. If not, you can create a new artist, and add it to the list of clickable points:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
pickable_artists = []
pt, = ax.plot(0.5, 0.5, 'o') # 5 points tolerance
pickable_artists.append(pt)
def onclick(event):
if event.inaxes is not None and not hasattr(event, 'already_picked'):
ax = event.inaxes
remove = [artist for artist in pickable_artists if artist.contains(event)[0]]
if not remove:
# add a pt
x, y = ax.transData.inverted().transform_point([event.x, event.y])
pt, = ax.plot(x, y, 'o', picker=5)
pickable_artists.append(pt)
else:
for artist in remove:
artist.remove()
plt.draw()
fig.canvas.mpl_connect('button_release_event', onclick)
plt.show()
希望这可以帮助您实现梦想.:-)
Hope this helps you achieve your dream. :-)