您可以根据集群为每个节点分配颜色。 Matplotlib 的plt.get_cmap() 可以表示一系列颜色。 norm 告诉我们如何将聚类值映射到该颜色范围。可选地,可以添加一个颜色条来显示对应关系。
为了简单地显示分布,可以使用聚类值绘制histogram。
下面的示例使用稍微调整的参数来创建图表。
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
import networkx as nx
g = nx.erdos_renyi_graph(50, 0.1, seed=None, directed=False)
gc = g.subgraph(max(nx.connected_components(g)))
lcc = nx.clustering(gc)
cmap = plt.get_cmap('autumn')
norm = plt.Normalize(0, max(lcc.values()))
node_colors = [cmap(norm(lcc[node])) for node in gc.nodes]
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 4))
nx.draw_spring(gc, node_color=node_colors, with_labels=True, ax=ax1)
fig.colorbar(ScalarMappable(cmap=cmap, norm=norm), label='Clustering', shrink=0.95, ax=ax1)
ax2.hist(lcc.values(), bins=10)
ax2.set_xlabel('Clustering')
ax2.set_ylabel('Frequency')
plt.tight_layout()
plt.show()