简体   繁体   中英

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM