我正在用Holoviews绘制和选择点

import holoviews as hv
import numpy as np

N = 100
x = np.random.normal(size=N)
y = np.random.normal(size=N)

points = hv.Points((x, y))

selection = hv.streams.Selection1D(points)

points.options(tools=["lasso_select"])


如何在我的Python环境中获取从套索中选择的索引作为向量进行进一步分析?

最佳答案

有足够的文档,例如从这里开始:http://holoviews.org/reference/streams/bokeh/Selection1D_tap.html

基本上,您需要通过DynamicMap将选择流链接到holoviews元素。然后,selection将保存您选择的索引。

我根据文档改编了以下示例:

import holoviews as hv
import numpy as np
hv.extension('bokeh')

N = 100
x = np.random.normal(size=N)
y = np.random.normal(size=N)

points = hv.Points((x, y))

selection = hv.streams.Selection1D(source=points, index=[0]) # set default arg

def process_selection(index):
    print(index)
    return hv.VLine(np.mean(x[index]))


dmap = hv.DynamicMap(process_selection, streams=[selection])

l = points * dmap

l.options(hv.opts.Points(tools=['tap'], size=10))


然后做一些选择。现在print(selection)将保存选定的索引

关于python - 如何在Holoviews中访问通过流选择的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55864292/

10-10 21:51