How can I access values selected through a Stream in Holoviews?

谁说胖子不能爱 提交于 2019-12-13 03:55:54

问题


I'm plotting and selecting points with 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"])

How can I get the indices selected from lasso as a vector in my Python environment for further analysis?


回答1:


There's ample documentation, start for example here: http://holoviews.org/reference/streams/bokeh/Selection1D_tap.html

Basically, you need to link your selection stream to the holoviews element through a DynamicMap. Then, selection will hold your selected indices.

I adapted the following example from the docs:

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))

Then do some selection. Now print(selection) will hold the selected indices



来源:https://stackoverflow.com/questions/55864292/how-can-i-access-values-selected-through-a-stream-in-holoviews

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!