Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to plot a dot each time at the point the mouse is clicked in matplotlib

How is it possible to plot a dot/circle at the point being clicked on in matplotlib(python 3)? Moreover i want to read the pixel value of each dot. I have tried plotting the circles but each time i read a different coordinate value, the previous circle is overwritten by the new circle. thank you

like image 336
Satyam Raha Avatar asked Jan 24 '17 09:01

Satyam Raha


People also ask

How do I plot multiple things in matplotlib?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.


1 Answers

You have to register an onclick event with the figure. Below is an example that plots a dot where the user clicks:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])

def onclick(event):
    print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
          (event.button, event.x, event.y, event.xdata, event.ydata))
    plt.plot(event.xdata, event.ydata, ',')
    fig.canvas.draw()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
like image 60
J. P. Petersen Avatar answered Oct 02 '22 14:10

J. P. Petersen