2013-03-08 36 views
3

我有2個圖形和一個鼠標運動功能,打印出他們的畫布的座標。我怎樣才能使鼠標運動函數只在鼠標懸停在其中一個圖表上時被調用。2圖鼠標運動matplotlib

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) 

此刻當鼠標沿畫布移動該被調用時,如果它不是在任圖表none被印刷爲座標。我怎樣才能做到這一點,所以它只被稱爲其中一個圖表

+0

您可以連接並在此基礎上軸鼠標斷開你的動作捕捉功能已經進入/左(其自身是連接,能事件) – RodericDay 2013-03-08 16:24:53

回答

1

從你的例子中不清楚,但我假設你有單獨的軸/子圖。 (如果情況並非如此,那麼您需要做更多的工作。)

在這種情況下,最簡單的方法是使用event.inaxes來檢測事件處於哪個軸。

作爲一個簡單的例子:

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