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

匆匆过客 提交于 2021-01-29 08:01:01

问题


I am using Holoviews to display a Sankey Diagram and would like to customize the information displayed when positioning a cursor over the diagram. However, I don't know how to display the correct labels.

Taking the 2nd example from the docs, I can add a custom 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]))

Same as in the example shown in the docs, the HoverTool displays the index values for "From" and "To" (e.g. "0", "1") etc., which do not necessarily mean anything to the user.

Is there a way to display the associated label (e.g. "PhD", "Career Outside Science", ...) in the HooverTool syntax?

I am using Holoviews 1.11.2 and Bokeh 1.0.4.


回答1:


The easiest way to do this is simply to provide the labels instead of the indices to the Sankey element:

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

That said I think your expectation that defining labels would make it to use the label column in the nodes to fetch the edge hover labels makes sense and labels may not be unique, so the approach above is not generally applicable. I'll file an issue in HoloViews.



来源:https://stackoverflow.com/questions/55327798/how-to-display-dataset-labels-inside-a-hovertool-in-a-sankey-diagram-using-holov

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