我有一个加权网络图。我已经计算了图上的特征向量中心性值;但是,我还想在每个节点顶部添加圆圈,以直观地表示特定节点的特征向量值。根据相应节点的特征向量值,圆圈的大小会有所不同。 (值越大,节点顶部的圆越大)。
我使用以下代码:
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'
)
最佳答案
您可以通过 matplotlib.pyplot.text
获得您想要的东西和 matplotlib.pyplot.Circle
。您可能希望进行稍微不同的调整,但希望这是一个好的起点:
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
}
plt.figure(figsize=(20,20))
ax = plt.gca()
ax.set_aspect('equal')
pos = nx.spring_layout(G)
nx.draw(G,
pos=pos,
node_color='lightblue',
labels=labels,
with_labels=True)
for node, (x,y) in pos.items():
rad = centrality[node]*0.16
circle = plt.Circle((x,y+rad), radius=rad, color='orange')
plt.text(x-.012, y+rad, node, fontsize=16, weight="bold")
ax.add_artist(circle)
plt.show()
关于python - 在 NetworkX 中的节点顶部显示可变大小的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64727486/