python - Draw network and grouped vertices of the same community or partition -
i need view (drawn or plot) communities structure in networks
i have this:
import igraph 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 vertex in g.vs(): vertex["label"] = vertex.index if membership not none: colors = [] in range(0, max(membership)+1): colors.append('%06x' % randint(0, 0xffffff)) 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 vertices spread. in networks result worse.
i want vertices grouped color in similar region.
e.g:
based on @gabor-csardi answer, made code:
import igraph random import randint def _plot(g, membership=none): if membership not none: gcopy = g.copy() edges = [] edges_colors = [] 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"] vertex in g.vs(): vertex["label"] = vertex.index if membership not none: colors = [] in range(0, max(membership)+1): colors.append('%06x' % randint(0, 0xffffff)) 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:
Comments
Post a Comment