Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a tree-like graph with root node at the top

Tags:

python

igraph

I have the following toy graph that represents, for instance, a forum thread:

import igraph as ig
g = ig.Graph(n = 12, directed=True)
g.add_edges([(1,0),(2,1), (3,2), (4,3),
             (5,1),
             (6,2), (7,6), (8,7),
             (9,0),
             (10,0), (11,10)])
g.vs["label"] = ["A", "B", "A", "B", "C", "F", "C", "B", "D", "C", "D", "F"]
ig.plot(g, layout="kk")

However, there seems to be no layout that places the root vertex (id 0, label A) into the top and grows downwards.

Am I missing something?

like image 663
alberto Avatar asked Dec 06 '25 00:12

alberto


1 Answers

OK, I'll just add this as an answer, for the comments.

So the Reingold-Tilford layout works: http://igraph.sourceforge.net/doc/python/igraph.Graph-class.html#layout_reingold_tilford

layout = g.layout_reingold_tilford(mode="in", root=[0])
like image 183
Gabor Csardi Avatar answered Dec 08 '25 16:12

Gabor Csardi