如何使用igraph for python绘制基于社区的图 [英] How to plot Community-based graph using igraph for python

查看:205
本文介绍了如何使用igraph for python绘制基于社区的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形,可以使用Louvain-Algorithm实现从中提取社区:

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]

最后,我绘制了该图:

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

我得到以下结果:

  • 我的问题是:

  • My question is :

  1. 有没有办法使用特定的布局来绘制按自身分组的四个社区中的每一个?我想将图表中不同区域的每个社区分开,以增加其内部结构的可见性,以及连接社区的中间度和中心性更高的少数边缘?

  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?

我使用了contract-vertices函数,该函数可以帮助我进行可视化,但是这过于简化了,它只是将每个社区都集中在一个点上,而无法可视化每个社区的内部结构.我是否以最佳方式使用合同顶点"?

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?

谢谢.

推荐答案

我发现解决方案是显着增加属于社区的边的权重(在以下示例中为顶点数的3倍):

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)

我认为在社区中需要急剧增加"边缘权重是由于以下事实:我的图由一些顶点组成,这些顶点表示的顶点数量少于2%,但连接的边缘超过80%即使他们属于不同的社区.在下图中,许多 社区外部的边缘为浅灰色:

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:

这篇关于如何使用igraph for python绘制基于社区的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆