Kristina Vakarov
Kristina Vakarov

Reputation: 191

Python Igraph Infomap node labels on graph

I have graph showed on first image, and I used infomap from igraph to make another, but on it I dont have node labels and that is so important fo me. How to add them, I tried everything but no node labels. It is strange that prints name of nodes like this,but no labels on graph:

IGRAPH U--- 86 41 -- + edges: 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 Also,how to save that community graph to png or similar?

Code: graph one community_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)

Upvotes: 1

Views: 2333

Answers (1)

deeenes
deeenes

Reputation: 4576

You have two options to show labels on an igraph plot: either pass vertex_label parameter to igraph.plot(), or set the label vertex attribute, and igraph will use that automatically at plotting. To save the figure, simply call the save() method of the plot object. The format will be set automatically by the extension.

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

Or creating the label vertex attribute:

g.vs['label'] = labels
igraph.plot(g)

Or if you want the indices to be used as labels:

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

Upvotes: 3

Related Questions