Search code examples
pythonbokehnetworkx

Bokeh hover highlighted edges change by mouse movement


I have this problem with Bokeh that hover highlighted edges change by mouse movement. It shows some of them when the mouse is on say north of the node, and some other when the mouse is on the south. I want all of a node's edges highlighted when the mouse is hovering over that node.

Here's the example:

import networkx as nx

from bokeh.models import Range1d, MultiLine, Circle, HoverTool
from bokeh.models.graphs import from_networkx, EdgesAndLinkedNodes
from bokeh.plotting import figure, show

G=nx.karate_club_graph()

plot = figure(plot_width=400, plot_height=400,
            x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot.add_tools(HoverTool(tooltips=None))

r = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))

r.node_renderer.glyph = Circle(size=15, fill_color='#2b83ba')
r.node_renderer.hover_glyph = Circle(size=15, fill_color='#abdda4')

r.edge_renderer.glyph = MultiLine(line_alpha=0, line_width=5)  # zero line alpha
r.edge_renderer.hover_glyph = MultiLine(line_color='#abdda4', line_width=5)

r.inspection_policy = EdgesAndLinkedNodes()
plot.renderers.append(r)

show(plot)

And two screenshots of the problem:

enter image description here

You cannot see the mouse in the screentshots, I don't know why, but the mouse is on the left-side node at both screenshots. With just a little movement, the highlighted edges change.


Solution

  • You should check

    from bokeh.models.graphs import NodesAndLinkedEdges instead of EdgesAndLinkedNodes

    So, r.inspection_policy = NodesAndLinkedEdges()

    This way your main focus is on nodes, not on edges. Your current solution hovers all edges, and although they are close to a node they might not be close enough to be all of them selected.