Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 graphs mouse motion matplotlib

I have 2 graphs and a mouse motion function that prints out the coordinates of the canvas they are on. How can I make it so that the mouse motion function is only called when,the mouse hovers over one of the graphs.

self.ax.imshow(matrix,cmap=plt.cm.Greys_r, interpolation='none')
self.ax.imshow(matrix2,cmap=plt.cm.Greys_r, interpolation='none')

def motion_capture(event)
    print event.xdata
    print event.ydata


self.canvas = FigureCanvas(self,-1,self.fig)
self.canvas.mpl.connect('Motion', motion_capture)

At the moment this is called when the mouse is moving along the canvas, if it is not on either graphs none is printed for the coordinates. How can I make it so it only is called for one of the graphs

like image 738
John Smith Avatar asked Mar 08 '26 16:03

John Smith


1 Answers

It isn't clear from your example, but I'm assuming you have separate axes/subplots. (If that's not the case, then you'll need to do a bit more.)

In that case, it's easiest to just detect which axes the event is in using event.inaxes.

As a quick example:

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(ncols=2)

axes[0].imshow(np.random.random((10,10)), interpolation='none')
axes[1].imshow(np.random.random((10,10)), interpolation='none')

def motion_capture(event):
    if event.inaxes is axes[0]:
        print event.xdata
        print event.ydata


fig.canvas.mpl_connect('motion_notify_event', motion_capture)

plt.show()
like image 92
Joe Kington Avatar answered Mar 11 '26 05:03

Joe Kington



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!