Building communities with python igraph

I have a g graph in python-igraph. I can get the VertexCluster community VertexCluster with the following:

 community = g.community_multilevel() 

community.membership gives me a list of group members of all the vertices of the graph.

My question is very simple, but I did not find an answer in Python on SO. How can I plot with visualization of its community structure? Preferred for PDF, so something like

 layout = g.layout("kk") plot(g, "graph.pdf", layout=layout) # Community detection? 

Thank you very much.

+6
source share
3 answers

You can pass the VertexClustering object directly to the chart function; it will automatically build the main graph and automatically select colors for clusters. The desired layout can be specified in the argument layout = ... as usual.

+6
source

The vertices remain ordered in layout , graph and VertexCluster , so you can do something like this:

Find the number of communities in the community structure:

 >>> max(community.membership) 10 

Then create a list / dictionary with max + 1 unique colors (maybe not as shown below):

 >>> color_list = [ ... 'red', ... 'blue', ... 'green', ... 'cyan', ... 'pink', ... 'orange', ... 'grey', ... 'yellow', ... 'white', ... 'black', ... 'purple' ... ] 

Then, using list comprehension, create a list containing the colors for each vertex, based on the group membership of that vertex, and assign this vertex_color :

 plot(g, "graph.png", layout=layout, vertex_color=[color_list[x] for x in community.membership]) 

The result (it is so beautiful!)

graph

+6
source

A good way to build communities is to use mark_groups :


An example :

 from igraph import * import random random.seed(1) g = Graph.Erdos_Renyi(30,0.3) comms = g.community_multilevel() plot(comms, mark_groups = True) 

The result is the following:

enter image description here

Hope this helps.

+4
source

All Articles