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.
A nice way to plot the communities could be the following using
mark_groups
:Example:
This results in the following:
Hope this helps.
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.
Vertices remain ordered in the
layout
,graph
, andVertexCluster
, so you can do something like this:Find the number of communities in the community structure:
Then create a list/dictionary with
max + 1
unique colors (probably not manually like below):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
:Result (It's so pretty!)