EoN.hierarchy_pos(G, root=None, width=1.0, vert_gap=0.2, vert_loc=0, leaf_vs_root_factor=0.5)[source]

If the graph is a tree this will return the positions to plot this in a hierarchical layout.

Based on Joel’s answer at https://stackoverflow.com/a/29597209/2966723, but with some modifications.

We include this because it may be useful for plotting transmission trees, and there is currently no networkx equivalent (though it may be coming soon).

There are two basic approaches we think of to allocate the horizontal location of a node.

  • Top down: we allocate horizontal space to a node. Then its k descendants split up that horizontal space equally. This tends to result in overlapping nodes when some have many descendants.
  • Bottom up: we allocate horizontal space to each leaf node. A node at a higher level gets the entire space allocated to its descendant leaves. Based on this, leaf nodes at higher levels get the same space as leaf nodes very deep in the tree.

We use use both of these approaches simultaneously with leaf_vs_root_factor determining how much of the horizontal space is based on the bottom up or top down approaches. 0 gives pure bottom up, while 1 gives pure top down.


G the graph (must be a tree)

root the root node of the tree - if the tree is directed and this is not given, the root will be found and used - if the tree is directed and this is given, then the positions will be

just for the descendants of this node.
  • if the tree is undirected and not given, then a random choice will be used.

width horizontal space allocated for this branch - avoids overlap with other branches

vert_gap gap between levels of hierarchy

vert_loc vertical location of root


xcenter: horizontal location of root