简体   繁体   中英

hvplot networkx node names with hover

I am using hvplot (version 0.4.0) with a networkx undirected graph (networkx version 2.1). When plotting the graph using the bokeh backend, I would like the hover to display the node name and not "index:number".

All the examples online in the docs have "index:number", I have tried to pass the names to the "labels" kwargs but that results in an error:

DataError: Supplied data does not contain specified dimensions, the following dimensions were not found:

import networkx as nx
import hvplot.networkx as hvnx
import holoviews as hv
hv.extension('bokeh')
GG = nx.Graph()
GG.add_edge('A','B')
GG.add_edge('B','C')
GG.add_edge('C','A')
hvnx.draw(GG)

在此输入图像描述

Looping through the GG object, provides the following info

for ii in GG.nodes():

    print(ii,type(ii))

A <class 'str'>
C <class 'str'>
B <class 'str'>

for ee in GG.edges():

    print(ee,type(ee))

('A', 'C') <class 'tuple'>
('A', 'B') <class 'tuple'>
('C', 'B') <class 'tuple'>

It seems like what you are trying to do should be the default behavior and likely represents some regression in HoloViews. That said the actual hover index data is actually being added to the plot it's just not referenced correctly. In your example you can make sure it is used correctly by explicitly declaring a bokeh HoverTool :

from bokeh.models import HoverTool

GG = nx.Graph()
GG.add_edge('A','B')
GG.add_edge('B','C')
GG.add_edge('C','A')
hvnx.draw(GG).opts(tools=[HoverTool(tooltips=[('index', '@index_hover')])])

在此输入图像描述

I have already filed an issue to make a note of this regression and you should expect this to be fixed in holoviews 1.12.0.

My solution

var selected_nodes = cb_data.source.selected["1d"].indices.map(function (selected_node_index) {
  return cb_data.source.data.index_hover[selected_node_index];
});


see cb_data.source.selected["1d"].indices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM