我有两个图形和一个鼠标移动功能,可以打印出他们所在的画布的坐标。如何才能使鼠标移动函数仅在鼠标悬停在其中一个图形上时才被调用。
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。我如何才能使它只为其中一个图调用
发布于 2013-03-09 01:08:17
从你的例子中并不清楚,但我假设你有单独的轴/子图。(如果不是这样,那么您需要做更多的工作。)
在这种情况下,最简单的方法就是使用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()https://stackoverflow.com/questions/15298444
复制相似问题