是否可以使用 PyVis 和 Python 显示网络边缘的权重?

时间:2021-04-26 20:34:07

标签: python networking graph graph-visualization pyvis

我已经阅读了文档并且我确实添加了带有属性 weight 的边,但是边的权重没有显示在图上,并且当我将一个curses悬停在两个节点之间的链接上时也不会显示它。可以显示重量吗?

代码片段(来源:https://pyvis.readthedocs.io/en/latest/tutorial.html#networkx-integration):

from pyvis.network import Network
import networkx as nx

nx_graph = nx.cycle_graph(10)
nx_graph.nodes[1]['title'] = 'Number 1'
nx_graph.nodes[1]['group'] = 1
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
nx_graph.nodes[3]['group'] = 10
nx_graph.add_node(20, size=20, title='couple', group=2)
nx_graph.add_node(21, size=15, title='couple', group=2)
nx_graph.add_edge(20, 21, weight=5)
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
nt = Network('500px', '500px')
# populates the nodes and edges data structures
nt.from_nx(nx_graph)
nt.show('nx.html')

结果: result of the snippet

如何显示边的权重?在这个例子中,有节点 20 和 21 的边的权重。

1 个答案:

答案 0 :(得分:0)

您可以为 title= 提供一个 add_edge() 参数,如果您将鼠标悬停在边缘上,它将显示带有标签的工具提示:

from pyvis.network import Network

g = Network()
g.add_node(0)
g.add_node(1)
g.add_edge(0, 1, value=5, title="42")  # weight 42
g.show('nx.html')

enter image description here

Source