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 β β βββΌ ... β βββΌ ... βββΌ ...