bigtree

πŸ“Š PlotΒΆ

Plotting methods for Trees.

Functions:

reingold_tilford(tree_node[,Β ...])

Algorithm for drawing tree structure, retrieves (x, y) coordinates for a tree structure.

bigtree.utils.plot.reingold_tilford(tree_node: T, sibling_separation: float = 1.0, subtree_separation: float = 1.0, level_separation: float = 1.0, x_offset: float = 0.0, y_offset: float = 0.0) NoneΒΆ

Algorithm for drawing tree structure, retrieves (x, y) coordinates for a tree structure. Adds x and y attributes to every node in the tree. Modifies tree in-place.

This algorithm[1] is an improvement over Reingold Tilford algorithm[2].

According to Reingold Tilford’s paper, a tree diagram should satisfy the following aesthetic rules,

  1. Nodes at the same depth should lie along a straight line, and the straight lines defining the depths should be parallel.

  2. A left child should be positioned to the left of its parent node and a right child to the right.

  3. A parent should be centered over their children.

  4. A tree and its mirror image should produce drawings that are reflections of one another; a subtree should be drawn the same way regardless of where it occurs in the tree.

>>> from bigtree import reingold_tilford, list_to_tree
>>> path_list = ["a/b/d", "a/b/e/g", "a/b/e/h", "a/c/f"]
>>> root = list_to_tree(path_list)
>>> root.show()
a
β”œβ”€β”€ b
β”‚   β”œβ”€β”€ d
β”‚   └── e
β”‚       β”œβ”€β”€ g
β”‚       └── h
└── c
    └── f
>>> reingold_tilford(root)
>>> root.show(attr_list=["x", "y"])
a [x=1.25, y=3.0]
β”œβ”€β”€ b [x=0.5, y=2.0]
β”‚   β”œβ”€β”€ d [x=0.0, y=1.0]
β”‚   └── e [x=1.0, y=1.0]
β”‚       β”œβ”€β”€ g [x=0.5, y=0.0]
β”‚       └── h [x=1.5, y=0.0]
└── c [x=2.0, y=2.0]
    └── f [x=2.0, y=1.0]
References

[1] Walker, J. (1991). Positioning Nodes for General Trees. https://www.drdobbs.com/positioning-nodes-for-general-trees/184402320?pgno=4

[2] Reingold, E., Tilford, J. (1981). Tidier Drawings of Trees. IEEE Transactions on Software Engineering. https://reingold.co/tidier-drawings.pdf

Parameters:
  • tree_node (BaseNode) – tree to compute (x, y) coordinate

  • sibling_separation (float) – minimum distance between adjacent siblings of the tree

  • subtree_separation (float) – minimum distance between adjacent subtrees of the tree

  • level_separation (float) – fixed distance between adjacent levels of the tree

  • x_offset (float) – graph offset of x-coordinates

  • y_offset (float) – graph offset of y-coordinates