如何解决Networkx在节点旁边显示特征向量中心值
我有一个圆形布局networkx加权图。我想知道在计算特征向量中心值之后,是否有一种方法可以在图上显示 其相应节点旁边?
我正在使用eigenvector centrality numpy function:
centrality = nx.eigenvector_centrality_numpy(G)
print([f"{node} {centrality[node]:0.3f}" for node in centrality])
解决方法
是的,您可以将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'
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。