ylnor
ylnor

Reputation: 4869

How to plot Community-based graph using igraph for python

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: enter image description here

Thanks.

Upvotes: 1

Views: 1978

Answers (1)

ylnor
ylnor

Reputation: 4869

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:

enter image description here

Upvotes: 2

Related Questions