networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersReturnsBackRef
forest_str(graph, with_labels=True, sources=None, write=None, ascii_only=False)

Parameters

graph : nx.DiGraph | nx.Graph

Graph to represent (must be a tree, forest, or the empty graph)

with_labels : bool

If True will use the "label" attribute of a node to display if it exists otherwise it will use the node value itself. Defaults to True.

sources : List

Mainly relevant for undirected forests, specifies which nodes to list first. If unspecified the root nodes of each tree will be used for directed forests; for undirected forests this defaults to the nodes with the smallest degree.

write : callable

Function to use to write to, if None new lines are appended to a list and returned. If set to the print function, lines will be written to stdout as they are generated. If specified, this function will return None. Defaults to None.

ascii_only : Boolean

If True only ASCII characters are used to construct the visualization

Returns

str | None :

utf8 representation of the tree / forest

Creates a nice utf8 representation of a directed forest

Examples

>>> graph = nx.balanced_tree(r=2, h=3, create_using=nx.DiGraph)
... print(nx.forest_str(graph)) ╙── 0 ├─╼ 1 │   ├─╼ 3 │   │   ├─╼ 7 │   │   └─╼ 8 │   └─╼ 4 │   ├─╼ 9 │   └─╼ 10 └─╼ 2 ├─╼ 5 │   ├─╼ 11 │   └─╼ 12 └─╼ 6 ├─╼ 13 └─╼ 14
>>> graph = nx.balanced_tree(r=1, h=2, create_using=nx.Graph)
... print(nx.forest_str(graph)) ╙── 0 └── 1 └── 2
>>> print(nx.forest_str(graph, ascii_only=True))
+-- 0
    L-- 1
        L-- 2
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.readwrite.text.forest_str networkx.generators.trees.random_tree

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/readwrite/text.py#8
type: <class 'function'>
Commit: