如何绘制图形聚类系数的分布
How to plot the distribution of a graphs clustering coefficient
我是 networkx 和 pyplot 的新手,我只是想知道如何绘制局部聚类系数的分布图。绘制度数分布更简单,因为 degree_histogram 函数会为您完成,但我不确定该怎么做。
g = nx.erdos_renyi_graph(1000, 0.02, seed = None, directed = False)
gc = g.subgraph(max(nx.connected_components(g)))
lcc = nx.clustering(gc)
您可以根据聚类为每个节点分配一种颜色。 Matplotlib 的 plt.get_cmap()
can indicate a range of colors. And a 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()
我是 networkx 和 pyplot 的新手,我只是想知道如何绘制局部聚类系数的分布图。绘制度数分布更简单,因为 degree_histogram 函数会为您完成,但我不确定该怎么做。
g = nx.erdos_renyi_graph(1000, 0.02, seed = None, directed = False)
gc = g.subgraph(max(nx.connected_components(g)))
lcc = nx.clustering(gc)
您可以根据聚类为每个节点分配一种颜色。 Matplotlib 的 plt.get_cmap()
can indicate a range of colors. And a 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()