In a matplotlib plot, how can I continuously read the coordinates of the mouse when it is moved, but without waiting for clicks? This is possible in matlab, and there is a mpld3 plugin to do almost exactly what I want, but I can't see how to actually access the coordinates from it. There is also the package mpldatacursor, but this seems to require clicks. Searching for things like "matplotlib mouse coordinates without clicking" did not yield answers.
Answers using additional packages such as mpld3 are fine, but it seems like a pure matplotlib solution should be possible.
This can be done by connecting to the motion_notify_event, mentioned briefly here in the matplotlib docs. This event fires whenever the mouse moves, giving the callback function a MouseEvent class to work with. This question has some relevant examples.
The MouseEvent class has attributes x, y, xdata, and ydata. The (x, y) coordinates in terms of your plot are given by xdata and ydata; x and y are in pixels. An example is given at cursor_demo.py in the matplotlib docs.
Here's a fairly small example:
import matplotlib.pyplot as plt
import numpy as np
def plot_unit_circle():
angs = np.linspace(0, 2 * np.pi, 10**6)
rs = np.zeros_like(angs) + 1
xs = rs * np.cos(angs)
ys = rs * np.sin(angs)
plt.plot(xs, ys)
def mouse_move(event):
x, y = event.xdata, event.ydata
print(x, y)
plt.connect('motion_notify_event', mouse_move)
plot_unit_circle()
plt.axis('equal')
plt.show()
The solution is very simple! We can ask matplotlib to notify us whenever a mouse event happens on the plot. We need to specify three things to achieve this:
Where matplotlib should send the event data? ( Here we defined a function to receive the data. The name is arbitrary it could be anything)
Pass the name of the event and function to matplotlib (we pass them to .connect method.)
It's as simple as that!
def on_mouse_move(event):
print('Event received:',event.x,event.y)
image= #your image
plt.imshow(image)
plt.connect('motion_notify_event',on_mouse_move)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With