我正在使用Bokeh创建NetworkX图形的交互式可视化.当我用鼠标悬停在该节点上时,我想要做的是显示连接到特定节点的所有边.
在Bokeh用户指南中,有一个例子或多或少地做了我想要的,但我对这个解决方案不满意有两个原因:
所以,我尝试了另一种方法:从头开始绘制所有边,但将其visible属性设置为False.然后创建一个字典,其中节点索引为键,并且连接到该节点的边列表为值.当节点被鼠标悬停时,该节点的边缘应该将其visible属性更改为True.它看起来或多或少像这样:
global glob_edges_by_node_index
edges_by_node = {}
for edge in G.edges(data=True): # create the segments (G is a preexisting NetworkX graph)
u,v,d = edge
x_0 = pos[u][0] # pos is a preexisting dictionary of x and y values for each node
y_0 = pos[u][1]
x_1 = pos[v][0]
y_1 = pos[v][1]
width = 0.03*d['weight']
highlit_edge = p.segment(x0=[x_0],x1=[x_1],y0=[y_0],y1=[y_1],color='#379bdd',line_width=width) # p is a preexisting Bokeh figure
highlit_edge.visible = …Run Code Online (Sandbox Code Playgroud)