gpt4 book ai didi

python - 如何说服visuals_at超越ViewBox?

转载 作者:行者123 更新时间:2023-12-04 13:38:17 24 4
gpt4 key购买 nike

我目前正在评估 vispy 满足我的交互式绘图需求。虽然感觉有点测试版,但我对它的速度印象深刻。此外,在 API 设计方面,它看起来很有希望。

我需要使用的一项功能是用鼠标选择绘图元素。发行版 ( 0.6.4 ) 中有一个示例 promise 可以做到这一点: examples/demo/scene/picking.py .不幸的是它对我不起作用。

它显示一个包含多行图形的单一窗口。我可以与整个情节进行交互,即缩放和移动,但我无法选择单独的线条。

如果我对相关代码段进行猴子调试(打印语句是我的,完整示例是 at github ):

@fig.connect
def on_mouse_press(event):
global selected, fig
if event.handled or event.button != 1:
return
if selected is not None:
selected.set_data(width=1)
selected = None
for v in fig.visuals_at(event.pos):
print(v)
if isinstance(v, vp.LinePlot):
selected = v
break
if selected is not None:
selected.set_data(width=3)
update_cursor(event.pos)

我收到 <ViewBox at 0x...>无论我点击哪里。 figvispy.plot.Fig实例是 not well documented .

我怎样才能做到这一点,即使 visuals_at超越 ViewBox并找到实际的 LinePlot实例?

最佳答案

在调用visuals_at 之前,有一种解决方法可以使 View 非交互式。
之后, View 可以再次设置为交互式。

可以在此处的 google 群组消息 workaround 中找到此解决方法

帖子是 2015 年的,所以这个问题似乎已经有一段时间了。

代码

所以添加到代码中

plt.view.interactive = False

在调用 fig.visuals_at 之前和之后做
plt.view.interactive = True

之后 on_mouse_press 的代码应如下所示:
def on_mouse_press(event):
global selected, fig
if event.handled or event.button != 1:
return
if selected is not None:
selected.set_data(width=1)
selected = None
plt.view.interactive = False
for v in fig.visuals_at(event.pos):
if isinstance(v, vp.LinePlot):
selected = v
break
plt.view.interactive = True
if selected is not None:
selected.set_data(width=3)
update_cursor(event.pos)

测试

Test Output

关于python - 如何说服visuals_at超越ViewBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60536014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com