Python Igraph 社区集群颜色

Kri*_*rov 5 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)
Run Code Online (Sandbox Code Playgroud)

图片

dee*_*nes 7

目前尚不清楚您是如何尝试为顶点分配颜色的。您应该知道 igraph 在删除和添加顶点或边时重新索引顶点和边。这种重新索引应该被认为是不可预测的,这是我们唯一知道索引始终从0n-1的事情,并且属性仍然分配给正确的顶点或边。考虑到这些,您可以在社区检测之前或之后进行删除,只需要为顶点属性分配颜色:

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)
Run Code Online (Sandbox Code Playgroud)

按社区着色的图表

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

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)
Run Code Online (Sandbox Code Playgroud)

颜色混乱的情节

但是g.vs['color']顶点属性中的颜色仍然是正确的,它们显示了簇,只有删除的顶点丢失了(来自深蓝色簇):

igraph.plot(g,
            vertex_color = g.vs['color']) # this is automatic, I am 
                                          # writing here explicitely only to be clear
Run Code Online (Sandbox Code Playgroud)

删除顶点后的聚类颜色


Kri*_*rov 0

我找到了解决方案。首先删除单个节点,然后转换为 igraph 并做社区。