Drawing#
NetworkX provides basic functionality for visualizing graphs, but its main goal is to enable graph analysis rather than perform graph visualization. In the future, graph visualization functionality may be removed from NetworkX or only available as an add-on package.
Proper graph visualization is hard, and we highly recommend that people
visualize their graphs with tools dedicated to that task. Notable examples of
dedicated and fully-featured graph visualization tools are
Cytoscape,
Gephi,
Graphviz and, for
LaTeX typesetting,
PGF/TikZ.
To use these and other such tools, you should export your NetworkX graph into
a format that can be read by those tools. For example, Cytoscape can read the
GraphML format, and so, networkx.write_graphml(G, path)
might be an appropriate
choice.
- More information on the features provided here are available at
matplotlib: http://matplotlib.org/
pygraphviz: http://pygraphviz.github.io/
Matplotlib#
Draw networks with matplotlib.
Examples#
>>> G = nx.complete_graph(5)
>>> nx.draw(G)
See Also#
|
Draw the graph G with Matplotlib. |
|
Draw the graph G using Matplotlib. |
|
Draw the nodes of the graph G. |
|
Draw the edges of the graph G. |
|
Draw node labels on the graph G. |
|
Draw edge labels. |
|
Draw the graph |
|
Draw the graph |
|
Draw a planar networkx graph |
|
Draw the graph |
|
Draw the graph |
|
Draw the graph |
|
Draw networkx graph |
Graphviz AGraph (dot)#
Interface to pygraphviz AGraph class.
Examples#
>>> G = nx.complete_graph(5)
>>> A = nx.nx_agraph.to_agraph(G)
>>> H = nx.nx_agraph.from_agraph(A)
See Also#
Pygraphviz: http://pygraphviz.github.io/
Graphviz: https://www.graphviz.org
DOT Language: http://www.graphviz.org/doc/info/lang.html
|
Returns a NetworkX Graph or DiGraph from a PyGraphviz graph. |
|
Returns a pygraphviz graph from a NetworkX graph N. |
|
Write NetworkX graph G to Graphviz dot format on path. |
|
Returns a NetworkX graph from a dot file on path. |
|
Create node positions for G using Graphviz. |
|
Create node positions for G using Graphviz. |
Graphviz with pydot#
Import and export NetworkX graphs in Graphviz dot format using pydot.
Either this module or nx_agraph can be used to interface with graphviz.
Examples#
>>> G = nx.complete_graph(5)
>>> PG = nx.nx_pydot.to_pydot(G)
>>> H = nx.nx_pydot.from_pydot(PG)
See Also#
pydot: erocarrera/pydot
Graphviz: https://www.graphviz.org
DOT Language: http://www.graphviz.org/doc/info/lang.html
|
Returns a NetworkX graph from a Pydot graph. |
|
Returns a pydot graph from a NetworkX graph N. |
|
Write NetworkX graph G to Graphviz dot format on path. |
|
Returns a NetworkX |
|
Create node positions using Pydot and Graphviz. |
|
Create node positions using |
Graph Layout#
Node positioning algorithms for graph drawing.
For random_layout()
the possible resulting shape
is a square of side [0, scale] (default: [0, 1])
Changing center
shifts the layout by that amount.
For the other layout routines, the extent is [center - scale, center + scale] (default: [-1, 1]).
Warning: Most layout routines have only been tested in 2-dimensions.
|
Arf layout for networkx |
|
Position nodes in two straight lines. |
|
Position nodes according to breadth-first search algorithm. |
|
Position nodes on a circle. |
|
Position nodes using the ForceAtlas2 force-directed layout algorithm. |
|
Position nodes using Kamada-Kawai path-length cost-function. |
|
Position nodes without edge intersections. |
|
Position nodes uniformly at random in the unit square. |
|
Returns scaled position array to (-scale, scale) in all axes. |
|
Return a dictionary of scaled positions keyed by node |
|
Position nodes in concentric circles. |
|
Position nodes using Fruchterman-Reingold force-directed algorithm. |
|
Position nodes using the eigenvectors of the graph Laplacian. |
|
Position nodes in a spiral layout. |
|
Position nodes in layers of straight lines. |
LaTeX Code#
Export NetworkX graphs in LaTeX format using the TikZ library within TeX/LaTeX.
Usually, you will want the drawing to appear in a figure environment so
you use to_latex(G, caption="A caption")
. If you want the raw
drawing commands without a figure environment use to_latex_raw()
.
And if you want to write to a file instead of just returning the latex
code as a string, use write_latex(G, "filename.tex", caption="A caption")
.
To construct a figure with subfigures for each graph to be shown, provide
to_latex
or write_latex
a list of graphs, a list of subcaptions,
and a number of rows of subfigures inside the figure.
To be able to refer to the figures or subfigures in latex using \\ref
,
the keyword latex_label
is available for figures and sub_labels
for
a list of labels, one for each subfigure.
We intend to eventually provide an interface to the TikZ Graph features which include e.g. layout algorithms.
Let us know via github what you’d like to see available, or better yet give us some code to do it, or even better make a github pull request to add the feature.
The TikZ approach#
Drawing options can be stored on the graph as node/edge attributes, or can be provided as dicts keyed by node/edge to a string of the options for that node/edge. Similarly a label can be shown for each node/edge by specifying the labels as graph node/edge attributes or by providing a dict keyed by node/edge to the text to be written for that node/edge.
Options for the tikzpicture environment (e.g. “[scale=2]”) can be provided via a keyword argument. Similarly default node and edge options can be provided through keywords arguments. The default node options are applied to the single TikZ “path” that draws all nodes (and no edges). The default edge options are applied to a TikZ “scope” which contains a path for each edge.
Examples#
>>> G = nx.path_graph(3)
>>> nx.write_latex(G, "just_my_figure.tex", as_document=True)
>>> nx.write_latex(G, "my_figure.tex", caption="A path graph", latex_label="fig1")
>>> latex_code = nx.to_latex(G) # a string rather than a file
You can change many features of the nodes and edges.
>>> G = nx.path_graph(4, create_using=nx.DiGraph)
>>> pos = {n: (n, n) for n in G} # nodes set on a line
>>> G.nodes[0]["style"] = "blue"
>>> G.nodes[2]["style"] = "line width=3,draw"
>>> G.nodes[3]["label"] = "Stop"
>>> G.edges[(0, 1)]["label"] = "1st Step"
>>> G.edges[(0, 1)]["label_opts"] = "near start"
>>> G.edges[(1, 2)]["style"] = "line width=3"
>>> G.edges[(1, 2)]["label"] = "2nd Step"
>>> G.edges[(2, 3)]["style"] = "green"
>>> G.edges[(2, 3)]["label"] = "3rd Step"
>>> G.edges[(2, 3)]["label_opts"] = "near end"
>>> nx.write_latex(G, "latex_graph.tex", pos=pos, as_document=True)
Then compile the LaTeX using something like pdflatex latex_graph.tex
and view the pdf file created: latex_graph.pdf
.
If you want subfigures each containing one graph, you can input a list of graphs.
>>> H1 = nx.path_graph(4)
>>> H2 = nx.complete_graph(4)
>>> H3 = nx.path_graph(8)
>>> H4 = nx.complete_graph(8)
>>> graphs = [H1, H2, H3, H4]
>>> caps = ["Path 4", "Complete graph 4", "Path 8", "Complete graph 8"]
>>> lbls = ["fig2a", "fig2b", "fig2c", "fig2d"]
>>> nx.write_latex(graphs, "subfigs.tex", n_rows=2, sub_captions=caps, sub_labels=lbls)
>>> latex_code = nx.to_latex(graphs, n_rows=2, sub_captions=caps, sub_labels=lbls)
>>> node_color = {0: "red", 1: "orange", 2: "blue", 3: "gray!90"}
>>> edge_width = {e: "line width=1.5" for e in H3.edges}
>>> pos = nx.circular_layout(H3)
>>> latex_code = nx.to_latex(H3, pos, node_options=node_color, edge_options=edge_width)
>>> print(latex_code)
\documentclass{report}
\usepackage{tikz}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\draw
(1.0, 0.0) node[red] (0){0}
(0.707, 0.707) node[orange] (1){1}
(-0.0, 1.0) node[blue] (2){2}
(-0.707, 0.707) node[gray!90] (3){3}
(-1.0, -0.0) node (4){4}
(-0.707, -0.707) node (5){5}
(0.0, -1.0) node (6){6}
(0.707, -0.707) node (7){7};
\begin{scope}[-]
\draw[line width=1.5] (0) to (1);
\draw[line width=1.5] (1) to (2);
\draw[line width=1.5] (2) to (3);
\draw[line width=1.5] (3) to (4);
\draw[line width=1.5] (4) to (5);
\draw[line width=1.5] (5) to (6);
\draw[line width=1.5] (6) to (7);
\end{scope}
\end{tikzpicture}
\end{figure}
\end{document}
Notes#
If you want to change the preamble/postamble of the figure/document/subfigure
environment, use the keyword arguments: figure_wrapper
, document_wrapper
,
subfigure_wrapper
. The default values are stored in private variables
e.g. nx.nx_layout._DOCUMENT_WRAPPER
References#
TikZ: https://tikz.dev/
TikZ options details: https://tikz.dev/tikz-actions
|
Return a string of the LaTeX/TikZ code to draw |
|
Return latex code to draw the graph(s) in |
|
Write the latex code to draw the graph(s) onto |