write_network_text#

write_network_text(graph, path=None, with_labels=True, sources=None, max_depth=None, ascii_only=False, end='\n', vertical_chains=False)[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

vertical_chainsBoolean

If True, chains of nodes will be drawn vertically when possible.

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.cycle_graph(5, nx.DiGraph)
>>> nx.write_network_text(graph, vertical_chains=True)
╙── 0 ╾ 4

    1

    2

    3

    4
    └─╼  ...
>>> nx.write_network_text(graph, vertical_chains=True, ascii_only=True)
+-- 0 <- 4
    !
    1
    !
    2
    !
    3
    !
    4
    L->  ...
>>> graph = nx.generators.barbell_graph(4, 2)
>>> nx.write_network_text(graph, vertical_chains=False)
╙── 4
    ├── 5
    │   └── 6
    │       ├── 7
    │       │   ├── 8 ─ 6
    │       │   │   └── 9 ─ 6, 7
    │       │   └──  ...
    │       └──  ...
    └── 3
        ├── 0
        │   ├── 1 ─ 3
        │   │   └── 2 ─ 0, 3
        │   └──  ...
        └──  ...
>>> nx.write_network_text(graph, vertical_chains=True)
╙── 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
    │   │   └─╼  ...
    │   └─╼  ...
    └─╼  ...