Python Igraph社区集群颜色

时间:2016-06-16 09:44:31

标签: python plot igraph

我希望在社区信息发布之后有不同颜色的集群,但问题是当我删除单个节点时会弄得一团糟,每个节点都是不同的颜色,或者一切都是红色的。如何在python中做到这一点?

代码:

E = ig.Graph(edges)
E.vs\['label'\] = labels
degree = 0
community = E.community_infomap()
cg = community.graph
singletons = cg.vs.select(_degree = 0)
cg.delete_vertices(singletons)
color_list =['red','blue','green','cyan','pink','orange','grey','yellow','white','black','purple' ]

ig.plot(cg)

image

2 个答案:

答案 0 :(得分:2)

目前尚不清楚您是如何尝试为顶点指定颜色的。您应该知道,在删除和添加顶点或边时,igraph会重新索引顶点和边。这种重新索引应该被认为是不可预测的,我们只知道索引始终从roles VARCHAR(10) NOT NULL CHECK (roles IN('Admin', 'Staff', 'User')) 0,并且属性仍然分配给正确的顶点或边。考虑到这些,您可以在社区检测之前或之后进行删除,只需要为顶点属性指定颜色:

n-1

graph colored by communities

现在让我们看看如果我们删除一个顶点会发生什么:

import igraph
g = igraph.Graph.Barabasi(n = 20, m = 1)
i = g.community_infomap()
pal = igraph.drawing.colors.ClusterColoringPalette(len(i))
g.vs['color'] = pal.get_many(i.membership)
igraph.plot(g)

plot with colors messed up

但是colors_original = pal.get_many(i.membership) g.delete_vertices([7]) # the clustering object is still the same length # (so it is not valid any more, you can't be sure # if the i.th vertex in the clustering is the # i.th one in the graph) len(i) # 20 # while the graph has less vertices g.vcount() # 19 # if we plot this with the original colors, indeed we get a mess: igraph.plot(g, vertex_color = colors_original) 顶点属性中的颜色仍然正确,它们显示了簇,只删除了已删除的顶点(来自深蓝色簇):

g.vs['color']

cluster colors after vertex deletion

答案 1 :(得分:0)

我找到了解决方案。首先删除单个节点而不是转换为igraph并进行社区。