如何使用Holoviews和Bokeh在Sankey图中的HoverTool内部显示数据集标签

时间:2019-03-24 19:39:55

标签: bokeh sankey-diagram holoviews

我正在使用Holoviews显示Sankey Diagram,并希望自定义将光标放在图表上方时显示的信息。但是,我不知道如何显示正确的标签。

the docs中的第二个示例为例,我可以添加自定义HoverTool

import holoviews as hv
from holoviews import opts
from bokeh.models import HoverTool 

nodes = ["PhD", "Career Outside Science",  "Early Career Researcher", "Research Staff",
         "Permanent Research Staff",  "Professor",  "Non-Academic Research"]
nodes = hv.Dataset(enumerate(nodes), 'index', 'label')
edges = [
    (0, 1, 53), (0, 2, 47), (2, 6, 17), (2, 3, 30), (3, 1, 22.5), (3, 4, 3.5), (3, 6, 4.), (4, 5, 0.45)   
]

value_dim = hv.Dimension('Percentage', unit='%')
careers = hv.Sankey((edges, nodes), ['From', 'To'], vdims=value_dim)

# this is my custom HoverTool
hover = HoverTool(
    tooltips = [
        ("From": "@From"), # this displays the index: "0", "1" etc. 
        ("To": "@To"), # How to display the label ("PhD", "Career Outside Science", ...)?
   ]
)

careers.opts(
    opts.Sankey(labels='label', tools=[hover]))

与示例shown in the docs中一样,HoverTool显示“从”和“到”的索引值(例如,“ 0”,“ 1”)等,但不一定表示给用户的任何东西。

是否可以使用HooverTool语法显示相关标签(例如,“ PhD”,“ Career Outside Outside”等)?

我正在使用Holoviews 1.11.2和Bokeh 1.0.4。

1 个答案:

答案 0 :(得分:0)

最简单的方法是提供标签而不是Sankey元素的索引:

nodes = ["PhD", "Career Outside Science",  "Early Career Researcher", "Research Staff",
         "Permanent Research Staff",  "Professor",  "Non-Academic Research"]
edges = [
    (0, 1, 53), (0, 2, 47), (2, 6, 17), (2, 3, 30), (3, 1, 22.5), (3, 4, 3.5), (3, 6, 4.), (4, 5, 0.45)   
]

# Replace the indices with the labels
edges = [(nodes[s], nodes[e], v) for s, e, v in edges]

value_dim = hv.Dimension('Percentage', unit='%')
careers = hv.Sankey(edges, ['From', 'To'], vdims=value_dim)
careers.opts(labels='index', tools=['hover'])

enter image description here

那就是说,我认为您期望定义labels将使其能够使用节点中的标签列来获取边缘悬停标签是有意义的,并且标签可能不是唯一的,因此上述方法通常不适用。我将在HoloViews中提出一个问题。