9

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

community = g.community_multilevel()

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

My question is really simple but I haven't found a python-specific answer on SO. How can I plot the graph with visualization of its community structure? Preferably to PDF, so something like

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

Thanks a lot.

seralouk
  • 30,938
  • 9
  • 118
  • 133
jayelm
  • 7,236
  • 5
  • 43
  • 61

3 Answers3

10

A nice way to plot the communities could be the following using mark_groups:


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)

This results in the following:

enter image description here

Hope this helps.

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • I get the following error when I try this: 'TypeError: plotting not available'. Can you clarify for me how to bypass this? Your graph seems really useful. – Laurie Jun 28 '18 at 10:56
  • 1
    hello. The error that you get is related to some dependencies that Igraph is using for plotting. See my answer here: https://stackoverflow.com/a/45416251/5025009 – seralouk Jun 28 '18 at 11:09
  • That didn't help, however for anyone thats interested I found the solution was installing Micsrosoft Visual C++ 14.0, and installing the Pycairo wheel file located here: https://www.lfd.uci.edu/~gohlke/pythonlibs/. Thanks for trying anyway. – Laurie Jun 28 '18 at 13:03
  • Hi Seralouk, I'd really appreciate if you could have a look at my post in the link below. Cheers. https://stackoverflow.com/questions/51102350/python-remove-overlapping-communities-in-igraph-plot – Laurie Jun 29 '18 at 13:37
9

Vertices remain ordered in the 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 (probably not manually like 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 that to vertex_color:

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

Result (It's so pretty!)

graph

jayelm
  • 7,236
  • 5
  • 43
  • 61
6

You can pass your VertexClustering object directly to the plot function; it will automatically plot the underlying graph instead and select colors automatically for the clusters. The desired layout can be specified in the layout=... keyword argument as usual.

Tamás
  • 47,239
  • 12
  • 105
  • 124