【问题标题】:Is it possible to display weight of edges of a network using PyVis and Python?是否可以使用 PyVis 和 Python 显示网络边缘的权重?
【发布时间】:2021-07-20 05:31:55
【问题描述】:

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

代码 sn-p(来源: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 的边的权重。

【问题讨论】:

  • 你能发布一个显示问题的最小代码sn-p吗?
  • 好的,我会更新的

标签: python networking graph graph-visualization pyvis


【解决方案1】:

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

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')

Source

【讨论】:

  • 我想过这个解决方案,但在我的网络中,几乎每个节点的度数都大于 1
  • @Remi 这有什么问题吗?标题显示在边缘,而不是节点上。
  • 啊,我很抱歉,你是对的。好像解决了我的问题,谢谢!
  • 很高兴能帮上忙 (:
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2019-03-07
相关资源
最近更新 更多