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

How to display Dataset labels inside a HoverTool in a Sankey diagram using Holoviews and Bokeh

我正在使用 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 显示 "From" 和 "To" 的索引值(例如“0”、“1”)等。 ,这不一定对用户有任何意义。

有没有办法在 HooverTool 语法中显示关联的标签(例如 "PhD"、"Career Outside Science"、...)?

我正在使用 Holoviews 1.11.2 和 Bokeh 1.0.4。

最简单的方法就是为 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'])

也就是说,我认为您期望定义 labels 会使用节点中的标签列来获取边缘悬停标签是有意义的,标签可能不是唯一的,所以上面的方法不是普遍适用。我将在 HoloViews 中提交问题。