write_network_text#

write_network_text(graph, path=None, with_labels=True, sources=None, max_depth=None, ascii_only=False, end='\n')[source]#

Creates a nice text representation of a graph

This works via a depth-first traversal of the graph and writing a line for each unique node encountered. Non-tree edges are written to the right of each node, and connection to a non-tree edge is indicated with an ellipsis. This representation works best when the input graph is a forest, but any graph can be represented.

Parameters:
graphnx.DiGraph | nx.Graph

Graph to represent

pathstring or file or callable or None

Filename or file handle for data output. if a function, then it will be called for each generated line. if None, this will default to β€œsys.stdout.write”

with_labelsbool | str

If True will use the β€œlabel” attribute of a node to display if it exists otherwise it will use the node value itself. If given as a string, then that attribute name will be used instead of β€œlabel”. Defaults to True.

sourcesList

Specifies which nodes to start traversal from. Note: nodes that are not reachable from one of these sources may not be shown. If unspecified, the minimal set of nodes needed to reach all others will be used.

max_depthint | None

The maximum depth to traverse before stopping. Defaults to None.

ascii_onlyBoolean

If True only ASCII characters are used to construct the visualization

endstring

The line ending character

Examples

>>> graph = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
>>> nx.write_network_text(graph)
╙── 0
    β”œβ”€β•Ό 1
    β”‚   β”œβ”€β•Ό 3
    β”‚   └─╼ 4
    └─╼ 2
        β”œβ”€β•Ό 5
        └─╼ 6
>>> # A near tree with one non-tree edge
>>> graph.add_edge(5, 1)
>>> nx.write_network_text(graph)
╙── 0
    β”œβ”€β•Ό 1 β•Ύ 5
    β”‚   β”œβ”€β•Ό 3
    β”‚   └─╼ 4
    └─╼ 2
        β”œβ”€β•Ό 5
        β”‚   └─╼  ...
        └─╼ 6
>>> graph = nx.cycle_graph(5)
>>> nx.write_network_text(graph)
╙── 0
    β”œβ”€β”€ 1
    β”‚   └── 2
    β”‚       └── 3
    β”‚           └── 4 ─ 0
    └──  ...
>>> graph = nx.generators.barbell_graph(4, 2)
>>> nx.write_network_text(graph)
╙── 4
    β”œβ”€β”€ 5
    β”‚   └── 6
    β”‚       β”œβ”€β”€ 7
    β”‚       β”‚   β”œβ”€β”€ 8 ─ 6
    β”‚       β”‚   β”‚   └── 9 ─ 6, 7
    β”‚       β”‚   └──  ...
    β”‚       └──  ...
    └── 3
        β”œβ”€β”€ 0
        β”‚   β”œβ”€β”€ 1 ─ 3
        β”‚   β”‚   └── 2 ─ 0, 3
        β”‚   └──  ...
        └──  ...
>>> graph = nx.complete_graph(5, create_using=nx.Graph)
>>> nx.write_network_text(graph)
╙── 0
    β”œβ”€β”€ 1
    β”‚   β”œβ”€β”€ 2 ─ 0
    β”‚   β”‚   β”œβ”€β”€ 3 ─ 0, 1
    β”‚   β”‚   β”‚   └── 4 ─ 0, 1, 2
    β”‚   β”‚   └──  ...
    β”‚   └──  ...
    └──  ...
>>> graph = nx.complete_graph(3, create_using=nx.DiGraph)
>>> nx.write_network_text(graph)
╙── 0 β•Ύ 1, 2
    β”œβ”€β•Ό 1 β•Ύ 2
    β”‚   β”œβ”€β•Ό 2 β•Ύ 0
    β”‚   β”‚   └─╼  ...
    β”‚   └─╼  ...
    └─╼  ...