Draw network and grouped vertices of the same comm

2019-02-24 22:14发布

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):
    layout = g.layout("kk")
    visual_style = {}
    visual_style["edge_color"] = "gray"
    visual_style["vertex_size"] = 30
    visual_style["layout"] = layout
    visual_style["bbox"] = (1024, 768)
    visual_style["margin"] = 40
    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__":
    karate = igraph.Nexus.get("karate")
    cl = karate.community_fastgreedy()
    membership = cl.as_clustering().membership
    _plot(karate, membership)

But the vertices are spread. In another networks this result is very worse.

enter image description here

I want the vertices are grouped by color in a similar region.

E.g:

enter image description here

3条回答
爷、活的狠高调
2楼-- · 2019-02-24 22:24

Remove the edges across multiple communities, calculate the layout without these edges, and then use it for the original graph.

查看更多
forever°为你锁心
3楼-- · 2019-02-24 22:30

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

查看更多
Juvenile、少年°
4楼-- · 2019-02-24 22:30

To group the vertices of a community together and highlight them you should use 'mark_groups=True'. See http://igraph.org/python/doc/igraph.clustering-pysrc.html#VertexClustering.plot

查看更多
登录 后发表回答