我希望使用下面的onmove函数在matplotlib中创建一个自定义悬停操作.将x和event.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)
前几天我回答过一个相关问题。
您的“点”(或设备)坐标转换取决于 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.html 和 http://matplotlib.sourceforge.net/users/event_handling.html
此外,艺术家(线条、补丁等)有一个您可能感兴趣的 contains 方法:http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.artist.Artist.contains。
| 归档时间: |
|
| 查看次数: |
2568 次 |
| 最近记录: |