因此,我们以不同的方式构建网络,但我刚刚使用来自 networkx 的 Bokeh 渲染网络解决了这个问题。
我这样做的方法是使用此处另一个问题中概述的lines_source方法,用我想要的networkx数据生成数据帧,它为您提供:
....
plot = figure(
plot_width=1100, plot_height=700,
tools=['tap','box_zoom', 'reset']
) # This is the size of the widget designed.
# This function sets the color of the nodes, but how to set based on the
# name of the node?
r_circles = plot.circle(
'x', 'y', source=nodes_source, name= "Node_list",
size="_size_", fill_color="_color_", level = 'overlay',
)
hover = HoverTool(
tooltips=[('Name', '@name'),('Members','@Members')],
renderers=[r_circles]
) # Works to render only the nodes tooltips
def get_edges_specs(_network, _layout):
d = dict(xs=[], ys=[], alphas=[],from_node=[],to_node=[])
weights = [d['weight'] for u, v, d in _network.edges(data=True)]
max_weight = max(weights)
calc_alpha = lambda h: 0.1 + 0.6 * (h / max_weight)
for u, v, data in _network.edges(data=True):
d['xs'].append([_layout[u][0], _layout[v][0]])
d['from_node'].append(u)
d['to_node'].append(v)
d['ys'].append([_layout[u][1], _layout[v][1]])
d['alphas'].append(calc_alpha(data['weight']))
return d
lines_source = ColumnDataSource(get_edges_specs(network, layout))
r_lines = plot.multi_line(
'xs', 'ys',
line_width=1.5, alpha='alphas', color='navy',
source=lines_source
) # This function sets the color of the edges
然后我生成了一个悬停工具来显示我想要的边缘信息,所以在我的例子中我想知道“来自节点”属性。我还想给它一个崇高的名字,所以工具提示将呈现“Whered_ya_come_from”
hover2 = HoverTool(
tooltips=[('Whered_ya_come_from','@from_node')],
renderers=[r_lines]
)
然后,我们实现它的方式之间的唯一区别是,您尝试将其作为情节的单个添加来完成,而我将它们一个接一个地绘制。
plot.tools.append(hover1)
# done to append the tool at the end because it has a problem getting
# rendered, as it depended on the nodes being rendered first.
plot.tools.append(hover2)
从那里,您可以将其导出或将其呈现为 HTML 文件(我的首选方法)。