当我给graph_renderer.inspection_policy = NodesAndLinkedEdges()
时,我失去了悬停在边缘上以突出显示它的能力。要具有此功能,我需要设置graph_renderer.inspection_policy = EdgesAndLinkedNodes()
;但是,如果这样做,我将失去在节点上悬停时显示的信息-它们显示为“ ???
”。我该如何解决?
import networkx as nx
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool, WheelZoomTool
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
G=nx.karate_club_graph()
plot = Plot(plot_width=400, plot_height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot.title.text = "Graph Interaction Demonstration"
plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())
graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1])
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
hover = HoverTool(tooltips=[("Name:", "@name")])
plot.add_tools(hover, TapTool(), BoxSelectTool(), WheelZoomTool())
'''
Below is the relevant line of code.
I can't have hoverinfo if I don't set "graph_renderer.inspection_policy = NodesAndLinkedEdges()"
But if I do, I cannot highlight an edge by hovering over it.
To be able to highlight an edge by hovering over it, I need to set "graph_renderer.inspection_policy = EdgesAndLinkedNodes()"
But if I do that, I get "???" when hovering over the nodes.
How can I keep both?
'''
graph_renderer.inspection_policy = NodesAndLinkedEdges()
graph_renderer.node_renderer.data_source.data['name'] = list(G.nodes)
plot.renderers.append(graph_renderer)
output_file("interactive_graphs.html")
show(plot)
This显示了使用hovertool的4种不同方式-
那么,是否不可能同时悬停以突出显示边缘和悬停以一起显示节点信息?