Holoviews,当使用多个KDIM时,无法派生选定的示例索引

2024-04-30 02:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

对于多个kdim,一个2d图是由其他kdim分组生成的,这非常好

使用Selection1D,SingleTap streams,当用户选择样本时,使用索引x,y参数调用DynamicMap回调。如果只有一个图,则该指数就足以得出Sample=f[index]

但是,如果屏幕截图中没有非轴kdims值,b=0,则无法直接导出所选样本,即:样本=f[索引,b]。有没有方便的办法

screenshot of HoloMap + DynamicMap

import holoviews as hv
import numpy as np
import pandas as pd
import panel as pn
hv.extension('bokeh')
pn.extension()


selected = []
N = 100
a = np.random.normal(size=N)
b = np.random.randint(low = 0, high=25, size=N)
c = range(0, N)

df = pd.DataFrame({'a':a, 'b':b, 'c':c})
ds = hv.Dataset(df, ['b', 'c'], 'a' )

points = ds.to(hv.Scatter, ['c'], 'a').opts(tools=['tap'], size=15)

# stream = hv.streams.Tap(source=points)
stream = hv.streams.Tap(source=points)
streamxy =  hv.streams.SingleTap(source=points)
streamI =  hv.streams.Selection1D(source=points)

def process_selection(index=0, a=0, b=0, c=0, x=0, y=0):

    #?? How can I find the selected sample ??

    # Couldn't find a way to find y, to be able to do:
    selected.append((index, a, b, c, x, y))

    if a is None: 
        a = 0
    if b is None: 
        b = 0
    if c is None: 
        c = 0


    return hv.Table({})


dmap = hv.DynamicMap(process_selection, streams=[streamI, streamxy], kdims=['b'])
dmap = dmap.redim.range(b=(0,25)).redim.range(c=(0,100))

points + dmap

Tags: toimportsourcesizeindexasnprange