79428777

Date: 2025-02-11 02:58:59
Score: 2
Natty:
Report link

In addition to Fff's answer, which reattaches the event handler after each plt.show(), I found two others that work:

It 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)
Reasons:
  • RegEx Blacklisted phrase (2.5): please let me know
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: dirtbirb