Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mouse coordinates without clicking in matplotlib

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.

like image 784
Robert D-B Avatar asked Feb 21 '26 20:02

Robert D-B


2 Answers

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()
like image 107
Robert D-B Avatar answered Feb 23 '26 08:02

Robert D-B


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:

  • Which mouse event we are interested in? ( Here we are interested in mouse move, not click for example) view supported events
  • 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()
like image 37
Mahmoud Avatar answered Feb 23 '26 08:02

Mahmoud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!