In my Matplotlib plot, I've created a number Circle artists that, when one is clicked, should result in having some information appear in an annotation. I have the basic code working except that I am seeing multiple calls to the handler occur even when the artist is only clicked one time. Normally, I'd try to debug this with the Python debugger except that it won't work in the Matplotlib event loop.
The handler code for this event is below. The artists parameter is a dict, keyed by the artist and holding a data structure as its value. This is the info to be put into the annotation.
def on_pick(event, artists):
artist = event.artist
print(f"pick event, {artist} class {artist.__class__}")
if artist in artists:
x = artist.center[0]
y = artist.center[1]
a = plt.annotate(f"{artists[artist].getX()}", xy=(x,y), xycoords='data')
artist.axes.figure.canvas.draw_idle()
print("done")
else:
print("on_pick: artist not found")
Each artist has it's picker attribute set to True and the on_pick function is installed as the handler for the pick event. When I run my program, the plot is created, and I click on one of the circles. I do see the annotation appear, as expected, but then I get a second, or more, invocation of the handler as seen here.
pick event, Circle(xy=(0.427338, 0.431087), radius=0.00287205) class <class 'matplotlib.patches.Circle'>
done
pick event, Circle(xy=(0.427338, 0.431087), radius=0.00287205) class <class 'matplotlib.patches.Circle'>
on_pick: artist not found
The first two lines are the correct ones while the second two are from the unexpected 2nd invocation. I'm also mystified as to why the artist isn't being found since its the exact same from the first call.
Any thoughts as to what might be happening?
Can't reproduce with the following, please provide a reproducible example.