检测matplotlib画布上的相对鼠标位置

mr_*_*_js 6 matplotlib

我希望使用下面的onmove函数在matplotlib中创建一个自定义悬停操作.将xevent.x中的现有数据点值转换为另一个坐标系(如点)的最佳方法是什么,以便我可以检测event.x何时在任何数据点的p点内?我知道选择器事件,但不想使用它,因为它基于点击,而不是悬停.

fig = figure()
ax1 = subplot(111)
x = linspace(0,10,11)
y = x**2
ax1.plot(x,y)
fig.canvas.mpl_connect('motion_notify_event', onmove)
p = 5

def onmove(event):
    if event.inaxes:
    #Detect if mouse was moved within p points of any value in x
Run Code Online (Sandbox Code Playgroud)

pel*_*son 3

前几天我回答过一个相关问题。

您的“点”(或设备)坐标转换取决于 x 和 y 的原始坐标系。如果 (x, y) 是轴 上的数据值,ax则可以使用 进行转换ax.transData.transform_point([x, y])。如果 (x, y) 位于轴坐标 (0-1) 中,那么这ax.transAxes就是您所追求的。

onmove 接收的事件将具有x和的属性y,这将是设备(像素)坐标中的 (x, y)

有关此信息的相关文档可以找到:http://matplotlib.sourceforge.net/users/transforms_tutorial.htmlhttp://matplotlib.sourceforge.net/users/event_handling.html

此外,艺术家(线条、补丁等)有一个您可能感兴趣的 contains 方法:http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.artist.Artist.contains