小编Mid*_*ler的帖子

Bokeh中的网络图形:在鼠标悬停在节点上时显示节点的连接

我正在使用Bokeh创建NetworkX图形的交互式可视化.当我用鼠标悬停在该节点上时,我想要做的是显示连接到特定节点的所有边.

在Bokeh用户指南中,有一个例子或多或少地做了我想要的,但我对这个解决方案不满意有两个原因:

  1. 在每个悬停事件中新绘制线段,这意味着它们显示节点圆上,看起来很难看.(这在示例中不明显,因为线和节点是相同的颜色).
  2. 我的图表是加权的,边缘的宽度取决于其重量.(据我所知)没有办法让突出显示的边缘达到正确的宽度.

所以,我尝试了另一种方法:从头开始绘制所有边,但将其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)

python graph callback bokeh

6
推荐指数
1
解决办法
1757
查看次数

标签 统计

bokeh ×1

callback ×1

graph ×1

python ×1