noiivice
noiivice

Reputation: 400

how can i show igraph outputs?

This is probably a very simple question. I'm new to python so would appreciate all the help!

In the codes I put below, how can I actually show the output instead of the memory object?

Graph.clusters(g)
Out[106]: <igraph.clustering.VertexClustering at 0x1187659d0>

Graph.community_edge_betweenness(g, clusters=None, directed=True, weights=None)
Out[107]: <igraph.clustering.VertexDendrogram at 0x118765d90>

Upvotes: 3

Views: 1717

Answers (1)

deeenes
deeenes

Reputation: 4576

It depends what exactly do you want to show? Let's take an example:

import igraph
g = igraph.Graph.Barabasi(n = 20, m = 3)
c = g.clusters()

print() in Python calls the __str__() method of the object, which converts it to something human readable, in case of a VertexClustering, each row represents a cluster (cluster ID in square brackets), and the vertex IDs belonging to that cluster are listed. The first line gives a simple description:

>>> print(c)
Clustering with 20 elements and 1 clusters
[0] 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

Then, you can access the members of each cluster as a list of vertex IDs like this:

>>> c[0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

In case of VertexDendrogram objects, igraph's print method even prints a nice text dendrogram:

>>> f = g.community_fastgreedy()
>>> print(f)
Dendrogram, 20 elements, 19 merges

7 3 14 10 5 16 1 0 9 8 6 2 4 18 12 13 19 15 17 11
| | |  |  | |  | | | | | | | |  |  |  |  |  |  |
`-' |  `--' |  | | | `-' | `-'  `--'  |  |  `--'
|  |   |   |  | | |  |  |  |    |    |  |   |  
`--'   |   |  `-' |  `--'  |    |    |  `---'  
|     |   |   |  |   |    |    |    |    |    
|     |   `---'  |   |    |    |    `----'    
|     |     |    |   |    |    |      |       
`-----'     `----'   |    `----'      |       
    |          |      |      |         |       
    |          `------'      `---------'       
    |             |               |            
    `-------------'               |            
            |                      |            
            `----------------------'

Finally, you can show your result using igraph's nice plotting capabilities:

i = g.community_infomap()
colors = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00"]
g.vs['color'] = [None]
for clid, cluster in enumerate(i):
    for member in cluster:
        g.vs[member]['color'] = colors[clid]
g.vs['frame_width'] = 0
igraph.plot(g)

Here we colored the vertices by their cluster (community) membership:

vertices colored by cluster membership

Upvotes: 4

Related Questions