【问题标题】:Detect relative mouse position on matplotlib canvas检测 matplotlib 画布上的相对鼠标位置
【发布时间】:2011-11-28 15:37:20
【问题描述】:

我希望使用下面的 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

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    我回复了related question the other day

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

    onmove 接收的事件将具有xy 的属性,这将是设备(像素)坐标中的 (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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-20
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2021-10-28
      相关资源
      最近更新 更多