首页 > 解决方案 > 如何访问通过 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 环境中获取从套索中选择的索引作为向量以进行进一步分析?

标签: pythonholoviews

解决方案


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


推荐阅读