Python-Igraph图上的Infomap节点标签

2024-09-29 17:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在第一张图上显示了这个图,我用igraph的infomap制作了另一张图,但在它上面我没有节点标签,这对我来说非常重要。如何添加它们,我尝试了一切,但没有节点标签。奇怪的是,像这样打印节点的名称,但图形上没有标签:

图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或类似文件中?在

代码: graph onecommunity_graph

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)

Tags: inposforlabels节点nprange标签
1条回答
网友
1楼 · 发布于 2024-09-29 17:22:12

您有两个选项可以在igraph图上显示标签:要么将vertex_label参数传递给igraph.plot(),要么设置labelvertex属性,igraph将在绘图时自动使用该属性。要保存图形,只需调用plot对象的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')

igraph plot with labels

或创建label顶点属性:

^{pr2}$

或者,如果要将索引用作标签:

igraph.plot(g, vertex_label = range(g.vcount()))

相关问题 更多 >

    热门问题