【问题标题】:How to change Nodes Size and Edge weight NetworkX?如何更改节点大小和边缘权重 NetworkX?
【发布时间】:2021-11-19 14:14:24
【问题描述】:

如何根据重量调整边缘厚度和节点大小?

import networkx as nx
import matplotlib.pyplot as plt


G_weighted = nx.Graph()
G_weighted.add_edge('abc','lifestyle', weight=5)
G_weighted.add_edge('abc','livestream', weight=3)
G_weighted.add_edge('abc','sony', weight=1)


plt.figure(figsize=(25,18))
nx.draw_networkx(G_weighted)

【问题讨论】:

    标签: python networkx social-networking


    【解决方案1】:

    需要设置widthnode_size属性:

    G_weighted.nodes['abc']['weight'] = 500
    G_weighted.nodes['lifestyle']['weight'] = 900
    G_weighted.nodes['livestream']['weight'] = 700
    G_weighted.nodes['sony']['weight'] = 1100
    
    plt.figure(figsize=(10,10))
    
    edge_weight = list(nx.get_edge_attributes(G_weighted,'weight').values())
    node_weight = list(nx.get_node_attributes(G_weighted,'weight').values())
    
    nx.draw_networkx(G_weighted, width=edge_weight, node_size=node_weight)
    

    figsize 的大参数自动缩放图形显示以适应屏幕的节点,因此我减少了它们以保持节点和边缘的自然大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      相关资源
      最近更新 更多