Matplotlib – add a point at every click

# draw one point using subplot
# draw a new point at every click 

import matplotlib.pyplot as plt

def onPress(event):
    print(event.xdata, event.ydata)
    xs = list(pnt.get_xdata())
    ys = list(pnt.get_ydata())
    xs.append(event.xdata)
    ys.append(event.ydata)
    pnt.set_data(xs, ys)
    pnt.figure.canvas.draw()
    
fig, ax = plt.subplots()
pnt, = ax.plot([0],[1], 'ro')
pnt.figure.canvas.mpl_connect('button_press_event', onPress)

plt.show()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s