【问题标题】:How to plot the distribution of a graphs clustering coefficient如何绘制图形聚类系数的分布
【发布时间】:2021-02-05 15:48:41
【问题描述】:

我是 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)

【问题讨论】:

    标签: python matplotlib cluster-analysis networkx


    【解决方案1】:

    您可以根据集群为每个节点分配颜色。 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()
    

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2021-11-25
      • 1970-01-01
      • 2020-12-13
      相关资源
      最近更新 更多