How to plot Community-based graph using igraph for python

旧街凉风 提交于 2019-12-08 04:14:01

问题


I have a graph from which I extract communities using Louvain-Algorithm implementation:

clusters = g.community_multilevel( weights=None, return_levels=False)

I then apply different colouring for each community:

new_cmap = ['#'+''.join([random.choice('0123456789abcdef') for x in range(6)]) for z in range(len(clusters))]
colors = {v: new_cmap[i] for i, c in enumerate(clusters) for v in c}
g.vs["color"] = [colors[e] for e in g.vs.indices]

Finally I plot the graph:

visual_style["layout"] = g.layout_fruchterman_reingold(weights=g.es["weight"], maxiter=1000, area=N ** 3, repulserad=N ** 3)
igraph.plot(g, **visual_style)

I get the bellow result:

  • My question is :

    1. Instead of this mixed-up graph, is there a way using a specific Layout, to plot every of the four community grouped by itself? I would like to separate every community in a different area of the graph increasing the visibility of their inner structure as well as the few edges with higher betweenness-centrality connecting the communities?

    2. I have used the contract-vertices function that helped me to visualise, but is an oversimplification that just group every community in a single point and doesn't allow to visualise the inner structure of each community. Am I using 'contract-vertices' in the best way?

Thanks.


回答1:


I found the solution being to drastically increase weight of edges which belong to the community (3 times the number of vertices in the below example):

clusters = g.community_multilevel( weights=None, return_levels=False)
member = clusters.membership
new_cmap = ['#'+''.join([random.choice('0123456789abcdef') for x in range(6)]) for z in range(len(clusters))]

vcolors = {v: new_cmap[i] for i, c in enumerate(clusters) for v in c}
g.vs["color"] = [vcolors[v] for v in g.vs.indices]

ecolors = {e.index: new_cmap[member[e.tuple[0]]] if member[e.tuple[0]]==member[e.tuple[1]] else "#e0e0e0" for e in g.es}
eweights = {e.index: (3*g.vcount()) if member[e.tuple[0]]==member[e.tuple[1]] else 0.1 for e in g.es}
g.es["weight"] = [eweights[e.index] for e in g.es]
g.es["color"] = [ecolors[e] for e in g.es.indices]

visual_style["layout"] = g.layout_fruchterman_reingold(weights=g.es["weight"], maxiter=500, area=N ** 3, repulserad=N ** 3)
igraph.plot(g, **visual_style)

I assume the need for 'drastically increase' edges weight within communities is due to the fact that my graph is composed of some vertices that represent less than 2% of the number of vertices but have more than 80% of edges connected to them even though they belong to different communities. In the below graph the many edges outside communities are in light grey:



来源:https://stackoverflow.com/questions/40443030/how-to-plot-community-based-graph-using-igraph-for-python

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