我在第一张图片上显示了图表,并且我使用了来自igraph的infomap制作另一张图片,但是在它上面我没有节点标签,这对我来说非常重要。如何添加它们,我尝试了所有但没有节点标签。很奇怪,打印这样的节点名称,但图上没有标签:
IGRAPH U --- 86 41 - +边缘: 0--57 65--67 2--66 2--67 2--23 66--67 57--68 68--81 6--66 33--74 74--84 37--74 32--76 57--81 32--84 37--84 70--84 32--85 58--85 22--24 22--23 23--31 24--25 24--31 30--31 32--33 32--57 42--48 42--46 42--55 43--48 43--55 43--47 44--49 46--48 46--55 47--54 47--55 49--51 54--55 57--58 另外,如何将社区图保存为png或类似的?
def nlargest_indices_orig(full, n):
full = full.copy()
x = np.zeros(n)
y = np.zeros(n)
for idx in range(n):
x[idx] = np.unravel_index(full.argmax(), full.shape)[0]
y[idx] = np.unravel_index(full.argmax(), full.shape)[1]
full[full == full.max()] = 0.
return x, y
labels=range(90)
o1 = scipy.io.loadmat('out.mat')
X=(o1['out'])
K=np.zeros((90,90))
m, n = np.shape(X)
print(m)
print(n)
G = nx.Graph()
#X[np.where(X<0.5)]=0
for i in range(90):
for j in range(90):
if X[i,j]>0:
s=labels[i]
b=labels[j]
w=X[i,j]
G.add_edge(s,b,weight=w)
B=G.edges()
print(len(B))
ND=len(B)
print('Grana ukupno')
print(ND)
procenat=round(0.1*ND)
print('procenat')
print(procenat)
x,y=nlargest_indices_orig(X, procenat)
s1=x
s2=y
for i in s1:
K[s1[i],s2[i]]=X[s1[i],s2[i]]
np.fill_diagonal(K, 0)
print('K')
print(K)
F = nx.Graph()
for i in range(90):
for j in range(90):
if K[i,j]>0:
s=labels[i]
b=labels[j]
w=X[i,j]
F.add_edge(s,b,weight=w)
edgewidth=[]
edgelabels={}
pos = nx.spring_layout(F) # position the nodes by force layout
plt.figure()
plt.axis('off')
print('Weighted graph')
for (u,v,d) in F.edges(data=True):
print(u,v,d)
edgewidth.append(d['weight'])
edgelabels[(u,v)] = d['weight']
nx.draw_networkx_edges(F,pos,width=edgewidth,edge_color='r')
nx.draw_networkx_nodes(F,pos, alpha=0.8, node_size=400,node_color='w',scale=100)
nx.draw_networkx_labels(F,pos, font_size=12)
pylab.savefig('Graf-s-10%.png')
plt.show()
A = F.edges()
E = ig.Graph(A)
print('E')
print(E)
community = E.community_infomap()
ig.plot( community,names=edgelabels)
答案 0 :(得分:0)
您有两个选项可以在igraph图上显示标签:将vertex_label
参数传递给igraph.plot()
,或设置label
顶点属性,igraph将在绘图时自动使用。要保存图形,只需调用绘图对象的save()
方法即可。格式将由扩展名自动设置。
import igraph
g = igraph.Graph.Barabasi(n = 8, m = 2)
labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
p = igraph.plot(g,
vertex_label = labels,
vertex_size = 32,
vertex_frame_width = 0.0,
vertex_color = '#AAAAFF')
p.save('plot1.png')
或创建label
顶点属性:
g.vs['label'] = labels
igraph.plot(g)
或者,如果您希望将索引用作标签:
igraph.plot(g, vertex_label = range(g.vcount()))