Search code examples
pythonnetwork-programmingplotcluster-computingigraph

How to plot only bigger communities with Igraph package in Python?


I am new in the networks world and I need help with the python package Igraph. I created my network and I applied community_walktrap() for the clustering. Now, I would like to plot just the clusters with more than 10 elements. Do you know how I can do this? To plot everything, I am using:

ig.plot(clusters, mark_groups = True, bbox=(1000,1000), vertex_label=g.vs['name'])

But I do not know if there is a way to exclude the smaller clusters.

Thank you in advace!


Solution

  • Unfortunately it's not as easy as it should be, but here's a quick sketch:

    def subgraph_of_large_clusters(clustering: VertexClustering, threshold: int) -> VertexClustering:
        sizes = clustering.sizes()
        members = [i for i, c in enumerate(clustering.membership) if sizes[c] >= threshold]
        new_graph = clustering.graph.subgraph(members)
        new_membership = [clustering.membership[i] for i in members]
        return VertexClustering(new_graph, new_membership)
    
    g = Graph.GRG(10000, 0.02)
    cl = g.community_walktrap().as_clustering()
    plot(subgraph_of_large_clusters(cl, 100))