【发布时间】:2020-11-09 12:39:38
【问题描述】:
我想在我的 NetworkX 圆形布局图中的节点外部显示特征向量值大小的圆圈。到目前为止,我能够在每个节点的顶部显示圆圈;但是,我想要每个节点外侧的圆圈。
对于左侧的节点,圆圈将位于节点的左侧。对于右侧的节点,在节点右侧圈出。节点在底部,圆圈将在节点下方。节点在顶部,圆圈将在节点的顶部。
我的代码:
import networkx as nx
import matplotlib.pyplot as plt
# 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.circular_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.25
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 matplotlib networkx