π PlotΒΆ
Plotting methods for Trees.
Functions:
|
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,
Nodes at the same depth should lie along a straight line, and the straight lines defining the depths should be parallel.
A left child should be positioned to the left of its parent node and a right child to the right.
A parent should be centered over their children.
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