python - Networkx 在节点旁边显示特征向量中心值
问题描述
我有一个圆形布局 networkx 加权图。我想知道在计算特征向量中心性值之后,是否有办法在图形上显示 其各自节点旁边的值?
我正在使用特征向量中心性 numpy 函数:
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'
)
推荐阅读
- azure - 无法使用 Nlog 将日志写入 Azure
- spring-boot - 加载资源失败 405
- java - URL 连接 Android 8.0.0
- matlab - 如何将用于图像有限差分导数逼近的 MATLAB 代码转换为一维信号?
- java - 即使 Junit 是绿色的,Wiremock 也显示零代码覆盖率
- powershell - 在Powershell中重新排列IF语句时的输出切换
- mongodb - 从 MongoDB 集合创建密钥列表 - Mongoosh / MongoDB 脚本
- odoo-14 - 在odoo14中删除自动完成
- .htaccess - .htaccess 的尾部斜杠问题
- go - 如何使用 go-git 包获取特定哈希的 git 标签以获取类似于 git describe --tags --always 的输出