如何访问Holoviews中的选定点?

时间:2019-04-26 09:08:04

标签: python holoviews

我正在使用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环境中获取从套索中选择的索引作为向量进行进一步分析?

1 个答案:

答案 0 :(得分:1)

有足够的文档,例如从这里开始: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)将保留选定的索引