📊 Plot
Plotting methods for Trees.
bigtree.utils.plot
reingold_tilford
reingold_tilford(
tree_node,
sibling_separation=1.0,
subtree_separation=1.0,
level_separation=1.0,
x_offset=0.0,
y_offset=0.0,
)
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.
Examples:
>>> 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:
Name | Type | Description | Default |
---|---|---|---|
tree_node |
BaseNode
|
tree to compute (x, y) coordinate |
required |
sibling_separation |
float
|
minimum distance between adjacent siblings of the tree |
1.0
|
subtree_separation |
float
|
minimum distance between adjacent subtrees of the tree |
1.0
|
level_separation |
float
|
fixed distance between adjacent levels of the tree |
1.0
|
x_offset |
float
|
graph offset of x-coordinates |
0.0
|
y_offset |
float
|
graph offset of y-coordinates |
0.0
|