1

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.

Petr Wolf
  • 127
  • 7

1 Answers1

2

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

enter image description here

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.

philippjfr
  • 3,997
  • 14
  • 15
  • Thanks, @philippjfr. Indeed, I wanted to use indexes because of non-unique labels, so this would not work in that particular use-case. – Petr Wolf Mar 27 '19 at 01:08
  • Okay, I'll try to get a fix in this week, which should make it into 1.12.0 due to be released this week. – philippjfr Mar 27 '19 at 14:42