为什么我的节点标签在使用 python-Louvain 时不显示?

Why are my node lables not displaying while using python-Louvain?

我正在尝试实现 Louvain 聚类算法并使用以下代码:

pip install python-louvain
import community.community_louvain
partition = community.community_louvain.best_partition(G)
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
plt.figure(figsize=(15,15))
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
                   cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.5, with_labels = True)
plt.show()

这确实输出了一个完美的社区聚类图,但是,我希望能够增加节点大小并打印节点标签

任何人都可以帮助我。

您可以使用 nx.draw_networkx_labels。你的情况:

nx.draw_networkx_labels(G, pos=pos)