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 环境中获取从套索中选择的索引作为向量以进行进一步分析?
解决方案
有充足的文档,例如从这里开始: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)
将保存选定的索引
推荐阅读
- php - 使用 mysqli 将 PHP 连接到 Google Cloud SQL 数据库时,用户名和密码使用什么?
- php - Magento 将 postDispatch 添加到 post 操作
- r - R:从另一个数据帧中找到最接近的较小值/日期
- python - 如何从生成器创建的 tf.data.Dataset 返回包含多个特征的字典?
- javascript - 在 JavaScript 中获取 CSS 动画的状态并将其打印在元素内或控制台上,以便稍后对其进行操作
- bash - 多变量到相同数据的 Bash 循环
- facebook - Facebook Messanger App Webhooks 仅 HTTPS?
- javascript - 在javascript中用多个$符号替换字符串
- assembly - 在 mandelbrot 集代码 SSE 汇编中找不到错误
- time-complexity - 几何级数平方和的复杂度