Plotting communities with python igraph

走远了吗. 提交于 2019-11-30 09:49:49

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, 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!)

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:

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!