Skip to content

Commit 763e66c

Browse files
authored
Merge pull request #296 from kayjan/enhance/docs
Add plotting to demonstration and installation instructions
2 parents a1135f5 + 9ae1e26 commit 763e66c

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Changed:
99
- Misc: Import module instead of functions, following Google Python Style Guide.
10+
- Docs: Documentation of `plot_tree` in tree demonstration and installation instructions.
1011

1112
## [0.21.0] - 2024-08-26
1213
### Added:

assets/demo/tree_plot.png

28.6 KB
Loading

docs/gettingstarted/demo/tree.md

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ Construct nodes with attributes. *DataFrame* can contain either <mark>path colum
322322
If tree is already created, nodes can still be added using path string, dictionary, and pandas/polars DataFrame!<br>
323323
Attributes can be added to existing nodes using a dictionary or pandas/polars DataFrame.
324324

325-
## Print Tree
325+
## View Tree
326+
327+
### 1. Print Tree
326328

327329
After tree is constructed, it can be viewed by printing to console using `show` or `hshow` method directly,
328330
for vertical and horizontal orientation respectively.
@@ -361,33 +363,33 @@ Other customizations for printing are also available, such as:
361363

362364
=== "Subtree"
363365
```python hl_lines="1 6"
364-
print_tree(root, node_name_or_path="b")
366+
root.show(node_name_or_path="b")
365367
# b
366368
# ├── d
367369
# └── e
368370

369-
print_tree(root, max_depth=2)
371+
root.show(max_depth=2)
370372
# a
371373
# ├── b
372374
# └── c
373375
```
374376
=== "Tree with attributes"
375377
```python hl_lines="1 8 15"
376-
print_tree(root, attr_list=["age"])
378+
root.show(attr_list=["age"])
377379
# a [age=90]
378380
# ├── b [age=65]
379381
# │ ├── d [age=40]
380382
# │ └── e [age=35]
381383
# └── c [age=60]
382384

383-
print_tree(root, attr_list=["age"], attr_bracket=["*(", ")"])
385+
root.show(attr_list=["age"], attr_bracket=["*(", ")"])
384386
# a *(age=90)
385387
# ├── b *(age=65)
386388
# │ ├── d *(age=40)
387389
# │ └── e *(age=35)
388390
# └── c *(age=60)
389391

390-
print_tree(root, all_attrs=True)
392+
root.show(all_attrs=True)
391393
# a [age=90, gender=F]
392394
# ├── b [age=65, gender=M]
393395
# │ ├── d [age=40, gender=F]
@@ -396,7 +398,7 @@ Other customizations for printing are also available, such as:
396398
```
397399
=== "Style - ansi"
398400
```python
399-
print_tree(root, style="ansi")
401+
root.show(style="ansi")
400402
# a
401403
# |-- b
402404
# | |-- d
@@ -405,7 +407,7 @@ Other customizations for printing are also available, such as:
405407
```
406408
=== "Style - ascii"
407409
```python
408-
print_tree(root, style="ascii")
410+
root.show(style="ascii")
409411
# a
410412
# |-- b
411413
# | |-- d
@@ -414,7 +416,7 @@ Other customizations for printing are also available, such as:
414416
```
415417
=== "Style - const"
416418
```python
417-
print_tree(root, style="const")
419+
root.show(style="const")
418420
# a
419421
# ├── b
420422
# │ ├── d
@@ -423,7 +425,7 @@ Other customizations for printing are also available, such as:
423425
```
424426
=== "Style - const_bold"
425427
```python
426-
print_tree(root, style="const_bold")
428+
root.show(style="const_bold")
427429
# a
428430
# ┣━━ b
429431
# ┃ ┣━━ d
@@ -432,7 +434,7 @@ Other customizations for printing are also available, such as:
432434
```
433435
=== "Style - rounded"
434436
```python
435-
print_tree(root, style="rounded")
437+
root.show(style="rounded")
436438
# a
437439
# ├── b
438440
# │ ├── d
@@ -441,7 +443,7 @@ Other customizations for printing are also available, such as:
441443
```
442444
=== "Style - double"
443445
```python
444-
print_tree(root, style="double")
446+
root.show(style="double")
445447
# a
446448
# ╠══ b
447449
# ║ ╠══ d
@@ -450,14 +452,45 @@ Other customizations for printing are also available, such as:
450452
```
451453
=== "Style - custom style"
452454
```python
453-
print_tree(root, style=("│ ", "├→ ", "╰→ "))
455+
root.show(style=("│ ", "├→ ", "╰→ "))
454456
# a
455457
# ├→ b
456458
# │ ├→ d
457459
# │ ╰→ e
458460
# ╰→ c
459461
```
460462

463+
### 2. Plot Tree
464+
465+
Tree can also be plotted using `plot` method directly with the help of `matplotlib` library.
466+
Alternatively, the `plot_tree` method can be used, but remember to run the `reingold_tilford` algorithm
467+
first to retrieve the *x* and *y* coordinates.
468+
469+
Arguments and keyword arguments can be passed in as long as they are compatible with the `plt.plot()`
470+
function. A *plt.Figure* object is returned if you want to do further customizations such as add title or
471+
save the figure to image.
472+
473+
```python hl_lines="9-10"
474+
from bigtree import Node, reingold_tilford, plot_tree
475+
476+
root = Node("a", age=90, gender="F")
477+
b = Node("b", age=65, gender="M", parent=root)
478+
c = Node("c", age=60, gender="M", parent=root)
479+
d = Node("d", age=40, gender="F", parent=b)
480+
e = Node("e", age=35, gender="M", parent=b)
481+
482+
reingold_tilford(root)
483+
fig = plot_tree(root, "-ok") # (1)!
484+
fig.axes[0].set_title("Tree Plot Demonstration")
485+
486+
fig.show() # Show figure
487+
fig.savefig("assets/demo/tree_plot.png") # Save figure
488+
```
489+
490+
1. Alternatively, `root.plot("-ok")` can be used
491+
492+
![Tree Plot Image Output](https://github.com/kayjan/bigtree/raw/master/assets/demo/tree_plot.png "Tree Plot Image Output")
493+
461494
## Tree Attributes and Operations
462495

463496
Note that using `BaseNode` or `Node` as superclass inherits the default class attributes (properties)

docs/home/install.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Examples of extra packages include:
3232

3333
- `all`: include all optional dependencies
3434
- `image`: for exporting tree to image
35+
- `matplotlib`: for plotting trees
3536
- `pandas`: for pandas methods
3637
- `polars`: for polars methods
3738

0 commit comments

Comments
 (0)