Networkx在节点旁边显示特征向量中心值

时间:2020-11-06 15:32:10

标签: python matplotlib networkx

我有一个圆形布局networkx加权图。我想知道在计算特征向量中心值之后,是否有一种方法可以在图上显示 其相应节点旁边

我正在使用eigenvector centrality numpy function

centrality = nx.eigenvector_centrality_numpy(G)

print([f"{node} {centrality[node]:0.3f}" for node in centrality])

1 个答案:

答案 0 :(得分:1)

是的,您可以将nx.eigenvector_centrality_numpy的结果设置为nx.draw函数的节点标签,因为nx.eigenvector_centrality_numpy为图形中的每个节点返回一个像{node: value}的字典,等于标签nx.draw使用的格式:

import networkx as nx

# Create a random graph
G = nx.gnp_random_graph(20, 0.2)

# Calculate centrality
centrality = nx.eigenvector_centrality_numpy(G)

# Create labels dict with fixed digit format
labels = {
    node: '{:.3f}'.format(centrality[node])
    for node in centrality
}

# Draw the graph with labels
nx.draw(
    G,
    with_labels=True,
    labels=labels,
    node_color='#FF0000'
)

enter image description here