【问题标题】:Is there a way to show number of connection between nodes and edges in networkx有没有办法显示networkx中节点和边缘之间的连接数
【发布时间】:2021-07-29 12:07:48
【问题描述】:

我在 networkx 中有双向图,我想显示两个节点之间有多少个连接。 如图所示,我想添加两个数字(或至少一个)来显示两个节点之间有多少连接。图表比这更复杂,因为这是示例问题。

Link to image

更新:

 df = pd.DataFrame({'from': fromlist, 'to': tolist})
 G = nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph())
 if.self.ui.radioButtonCircular.isChecked():
      nx.draw(G, with_labels=True, node_size=5500, node_color=[mapper.to_rgba(i)
      for i in d.values()], font_size=7, node_shape="s", alpha=0.7, arrows=True,
      pos=nx.circular_layout(G))
 plt.title("Directed")
 plt.show()

上面有很多分析,但是在我的fromlist和tolist中都是连接。像这样的:

fromlist: A, A, A, B, B, A, C
tolist:   B, C, B, A, A, B, A

现在我希望它在双向图中显示 A 与 B 连接 3 次,B 与 A 连接 2 次,C 与 A 连接一次,但 A 与 C 连接一次, ...在networkx中这可能吗?

【问题讨论】:

  • 到目前为止你能展示你的代码吗?或者至少是一个简化的例子?
  • @motrix 我用绘制的代码更新了我的问题......在此之前我有很多分析,但在我的 fromlist 和 tolist 中是连接

标签: python networkx


【解决方案1】:

DiGraphs 不允许像https://networkx.org/documentation/stable/reference/classes/digraph.html 中所述的平行边。
因此,如果您执行print(G['A']['B']),则只会显示一个边缘。

也许您可以尝试为边缘分配一个属性,例如:

for from_node, to_node in zip(from_list, to_list):
    if G.has_edge(from_node, to_node):
        G[from_node][to_node]['count'] += 1
    else:
        G.add_edge(from_node, to_node, count = 1)

然后你可以显示边缘属性如Labeling edges in networkx所示

如果您的图表非常大并且 for 循环花费太多时间,您可以这样做:

df['count'] = 1
df = df.groupby(['from', 'to']).sum().reset_index()
G = nx.from_pandas_edgelist(df, 'from', 'to', ['count'], create_using=nx.DiGraph())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多