Python Igraph 社区集群颜色

标签 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

最佳答案

目前尚不清楚您是如何尝试为顶点分配颜色的。您应该知道 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)

graph colored by communities

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

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)

plot with colors messed up

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

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

cluster colors after vertex deletion

关于Python Igraph 社区集群颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37855553/

相关文章:

r - 与另一个圆弧重叠时自动弯曲圆弧

r - 在同一位置绘制顶点

python - 极坐标中的 Matplotlib 密度图?

python - 向按年线分组的时间序列添加标签 - Matplotlib、Python

python - 使用带有 sklearn kmeans 的任意度量的文本聚类

python - 从html文档中提取标签内的文本

python - 如何平滑python中图形中的线条?

r - 在 R 中识别派系

Python Make 函数没有中断或继续

python - 如何在python中查找单词