Separate HoverTool for nodes and edges in graph

Hi!

I have similar problem as mentioned in an older question: Hover-over tooltips on network edges. I want to create separate HoverTool tooltips for edges and nodes in graph created in networkx. I have also found this solution on Stack Overflow python - Separate node and edge hover tools in Bokeh? - Stack Overflow.

I’m trying to add HoverTool to the nodes and edges renderers, but it does not work and I don’t know why. Here is an example:

import networkx as nx
from bokeh.plotting import figure, curdoc, from_networkx
from bokeh.models import HoverTool

# create networkx graph
g = nx.Graph()
g.add_nodes_from([(4,{"sheet":"A","coor":(2,1)}),
				  (5,{"sheet":"A","coor":(2,2)}),
				  (6,{"sheet":"A","coor":(3,1)}),
				  (7,{"sheet":"A","coor":(3,2)})])

g.add_edges_from([(4,5,{'weight':1, 'delay':1}),
				  (5,6,{'weight':1, 'delay':1}),
				  (4,6,{'weight':1, 'delay':1}),
				  (6,7,{'weight':1, 'delay':1})])

layout = {n:g.nodes[n]["coor"] for n in g.nodes()}

# create graph renderer from networkx graph
graph_renderer = from_networkx(g,layout)
 
plot = figure(x_range=(1,4), y_range=(0,3),
			  tools="lasso_select,pan,wheel_zoom")

# hover tools for edges and nodes
hover_nodes = HoverTool(
				tooltips=[("index", "@index"), ("coordinates", "@coor")],
				renderers=[graph_renderer.node_renderer]
				)

hover_edges = HoverTool(
				tooltips=[('weight','@weight'),('delay','@delay')],
				renderers=[graph_renderer.edge_renderer]
				)

plot.add_tools(hover_edges,hover_nodes)

plot.renderers.append(graph_renderer)
curdoc().add_root(plot)

Do you know how to solve it?

Thank you,
Katerina

Hi!
Same problem … any solution?

The OP code is working fine for me on latest Bokeh. Though it may the the case that the default line_policy value is not what you want. If you are expecting the tooltip to follow the mouse on the line segments, then you should configure line_policy="interp" on the edges hover tool.

Hello. I hope that and that when I stand above the edge some information about it will be seen. However, I have not been able to do this. I have added the line_policy to hover_edges

What do you propose?
import networkx as nx
from bokeh.plotting import figure, curdoc, from_networkx
from bokeh.models import HoverTool
from bokeh.io import output_notebook, show, save

create networkx graph

g = nx.Graph()
g.add_nodes_from([(4,{“sheet”:“A”,“coor”:(2,1)}),
(5,{“sheet”:“A”,“coor”:(2,2)}),
(6,{“sheet”:“A”,“coor”:(3,1)}),
(7,{“sheet”:“A”,“coor”:(3,2)})])

g.add_edges_from([(4,5,{‘weight’:1, ‘delay’:1}),
(5,6,{‘weight’:1, ‘delay’:1}),
(4,6,{‘weight’:1, ‘delay’:1}),
(6,7,{‘weight’:1, ‘delay’:1})])

layout = {n:g.nodes[n][“coor”] for n in g.nodes()}

create graph renderer from networkx graph

graph_renderer = from_networkx(g,layout)
TOOLTIPS=[(“index”, “@index”), (“coordinates”, “@coor”)]
plot = figure(tooltips = TOOLTIPS,x_range=(1,4), y_range=(0,3),
tools=“lasso_select,pan,wheel_zoom”)

hover_edges = HoverTool(
tooltips=[(‘weight’,’@weight’),(‘delay’,’@delay’)],
renderers=[graph_renderer.edge_renderer], line_policy=“interp”
)

plot.renderers.append(graph_renderer)
plot.add_tools(hover_edges)
show(plot)
I completed my question.

Thanks

@madegomez You have not provided enough information to speculate. Please provide a Minimal Reproducible Example of your code in a new issue, along with a detailed description of how it differs from your expectations (screenshots are probably helpful).

Hi! I completed my question. Thanks

1 Like