2016-06-16 28 views
1

コミュニティインフォマップの後に色が異なるクラスタを作成したいのですが、単一のノードを削除すると、各ノードが異なる色になるか、すべてが赤色になります。どのようにPythonでそれを行うには?Python Igraphコミュニティクラスタの色

コード:

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

答えて

0

私は解決策を見つけました。まずigraphに変換してコミュニティを行うよりも単一のノードを削除してください。

0

どのようにして頂点に色を割り当てようとしたのかは不明です。 igraphは、頂点または辺の削除と追加時に稜線を再インデックスすることに注意してください。この再インデックス化は予測不可能とみなされるべきであり、インデックスが常に0からn-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']頂点属性の色はまだcorreされていますCT、彼らは唯一の削除頂点(ダークブルークラスタから)失われ、クラスタを示しています。

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

関連する問題