Draw network and grouped vertices of the same community or partition

杀马特。学长 韩版系。学妹 提交于 2019-12-01 23:36:11

问题


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.

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

E.g:


回答1:


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:




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/23184306/draw-network-and-grouped-vertices-of-the-same-community-or-partition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!