如何使用Holoviews和Bokeh在Sankey图中的HoverTool内部显示数据集标签 [英] How to display Dataset labels inside a HoverTool in a Sankey diagram using Holoviews and Bokeh

查看:54
本文介绍了如何使用Holoviews和Bokeh在Sankey图中的HoverTool内部显示数据集标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Holoviews显示

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

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.

解决方案

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.

这篇关于如何使用Holoviews和Bokeh在Sankey图中的HoverTool内部显示数据集标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆