有没有办法在holoviews Sankey图中过滤尺寸

时间:2019-12-14 20:13:36

标签: python-3.x bokeh holoviews

我试图显示Holoviews中Sankey图中从位置的迁移,但是我找不到添加下拉式过滤器的方法。我不允许列出比我要绘制的键数更多的键数,我希望它在其他HoloViews元素中出现下拉菜单时会起作用,因为它会按我所做的所有键值自动将数据分组 not < / em>分配给元素。

import pandas as pd
import numpy as np
import holoviews as hv
from holoviews import opts

hv.extension('bokeh')

df = pd.DataFrame({'from': ["a", "b", "c", "a", "b", "c"],
                   'to': ["d", "d", "e", "e", "e", "d"],
                   'number': [10, 2, 1, 8, 2, 2],
                   'year': [2018, 2018, 2018, 2017, 2017, 2017]})
df

from    to  number  year
0   a   d   10  2018
1   b   d   2   2018
2   c   e   1   2018
3   a   e   8   2017
4   b   e   2   2017
5   c   d   2   2017

现在Holoviews将year列添加到kdims中,因为我希望下拉列表按年过滤:

kdims = ["from", "to", "year"]
vdims = ["number"]

sankey = hv.Sankey(df, kdims=kdims, vdims=vdims)
sankey.opts(label_position='left', edge_color='to', node_padding=30, node_color='number', cmap='tab20')

返回:

ValueError: kdims: list length must be between 2 and 2 (inclusive)

在没有第三个键维的情况下,Sankey图按预期工作,但是没有交互式过滤器: enter image description here

1 个答案:

答案 0 :(得分:2)

以下是解决问题的两种方法:

1)将数据框转换为holoviews数据集,并将其转换为Sankey图:
由于“ year”位于第三个键尺寸下方的代码中,因此它将用作滑块的尺寸。前两个变量(“ from”和“ to”)将用作Sankey图的调暗。

hv_ds = hv.Dataset(
    data=df, 
    kdims=['from', 'to', 'year'], 
    vdims=['number'],
)

hv_ds.to(hv.Sankey)


2)或者,每年创建一个Sankey地块字典并将其放入全息图:

sankey_dict = {
    year: hv.Sankey(df[df.year == year]) 
    for year in df.year.unique()
}

holo = hv.HoloMap(sankey_dict, kdims='year')


两种解决方案都可以创建全息图:
http://holoviews.org/reference/containers/bokeh/HoloMap.html


结果图+滑块holomap sankey slider


我已经测试过:
hvplot 0.5.2
holoviews 1.12.5和holoviews 1.13
jupyterlab 1.2.4