Draw network and grouped vertices of the same community or partition

前端 未结 3 1807
鱼传尺愫
鱼传尺愫 2021-01-20 18:59

I need view (drawn or plot) the communities structure in networks

I have this:

import igraph
from random import randint

def _plot(g, membership=None         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 19:46

    Based on @gabor-csardi answer, I made this code:

    import igraph
    from random import randint
    
    def _plot(g, membership=None):
        if membership is not None:
            gcopy = g.copy()
            edges = []
            edges_colors = []
            for edge in g.es():
                if membership[edge.tuple[0]] != membership[edge.tuple[1]]:
                    edges.append(edge)
                    edges_colors.append("gray")
                else:
                    edges_colors.append("black")
            gcopy.delete_edges(edges)
            layout = gcopy.layout("kk")
            g.es["color"] = edges_colors
        else:
            layout = g.layout("kk")
            g.es["color"] = "gray"
        visual_style = {}
        visual_style["vertex_label_dist"] = 0
        visual_style["vertex_shape"] = "circle"
        visual_style["edge_color"] = g.es["color"]
        # visual_style["bbox"] = (4000, 2500)
        visual_style["vertex_size"] = 30
        visual_style["layout"] = layout
        visual_style["bbox"] = (1024, 768)
        visual_style["margin"] = 40
        visual_style["edge_label"] = g.es["weight"]
        for vertex in g.vs():
            vertex["label"] = vertex.index
        if membership is not None:
            colors = []
            for i in range(0, max(membership)+1):
                colors.append('%06X' % randint(0, 0xFFFFFF))
            for vertex in g.vs():
                vertex["color"] = str('#') + colors[membership[vertex.index]]
            visual_style["vertex_color"] = g.vs["color"]
        igraph.plot(g, **visual_style)
    
    if __name__ == "__main__":
        g = igraph.Nexus.get("karate")
        cl = g.community_fastgreedy()
        membership = cl.as_clustering().membership
        _plot(g, membership)
    

    Results:

    enter image description here

提交回复
热议问题