在Bokeh网络图中,如何将鼠标悬停在边缘上以突出显示它,同时在悬停在节点上时能够同时显示hoverinfo?

时间:2019-05-20 03:16:02

标签: python data-visualization bokeh networkx

当我给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种不同方式-

  1. 使用悬停工具进行绘制,以突出显示连接到点的边缘
  2. 突出显示所选节点的边缘的图(单击)
  3. 突出显示与光标悬停的边缘相邻的节点的图
  4. 突出显示所选边的相邻节点的图(单击)

那么,是否不可能同时悬停以突出显示边缘和悬停以一起显示节点信息?

0 个答案:

没有答案