Search code examples
pythonmatplotlibcluster-analysisnetworkx

How to plot the distribution of a graphs clustering coefficient


I'm new to networkx and pyplot and I was just wondering how I would go about plotting a distribution of a local clustering coefficient. Plotting a degree distribution was more straightforward since the degree_histogram function does it for you, but with this I'm not sure what to do.

g = nx.erdos_renyi_graph(1000, 0.02, seed = None, directed = False)
gc = g.subgraph(max(nx.connected_components(g)))
lcc = nx.clustering(gc)

Solution

  • You could assign a color to each node depending on the clustering. Matplotlib's plt.get_cmap() can indicate a range of colors. And a norm tells how the clustering values will be mapped to that color range. Optionally, a colorbar can be added to show the correspondence.

    To simply show the distribution, a histogram can be drawn using the values of the clustering.

    The example below uses slightly adapted parameters to create the graph.

    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()
    

    example plot