In addition to Fff's answer, which reattaches the event handler after each plt.show()
, I found two others that work:
plt.draw()
at the end of the event handler (plt.draw_idle()
does not work)plt.ion()
and leave it that wayIt seems like toggling interactive mode off and on again confuses matplotlib such that it doesn't know where to draw stuff.
For now, I'm using plt.draw()
since that seems like the least complicated solution; attaching the same event multiple times seems less intuitive. If anyone has an explanation or a better solution, please let me know!
Solution for now:
import matplotlib.pyplot as plt
def onclick(event):
print(event.xdata, event.ydata)
plt.gca().scatter(event.xdata, event.ydata)
plt.draw() # draw_idle() doesn't work, but this does!
for i in range(2):
plt.plot([1, 2, 3, 4])
plt.gca().figure.canvas.mpl_connect('button_press_event', onclick)
with plt.ion():
plt.show(block=True)