Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layered layout drawing algorithm #4743

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion doc/reference/drawing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ Graph Layout
spectral_layout
spiral_layout
multipartite_layout

layered_layout
3 changes: 3 additions & 0 deletions doc/release/release_dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Improvements
- [`#4640 <https://github.com/networkx/networkx/pull/4640>`_]
``prefix_tree`` now uses a non-recursive algorithm. The original recursive
algorithm is still available via ``prefix_tree_recursive``.
- [`#4743 <https://github.com/networkx/networkx/pull/4743>`_]
New ``layered_layout`` graph positioning algorithm for directed acyclic
graphs based on Sugiyama's method.

API Changes
-----------
Expand Down
34 changes: 34 additions & 0 deletions examples/drawing/plot_layered_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
=================
Layered layout
=================

This example presents `networkx.drawing.layered_layout` node positioning algorithm, and how its output can be used with `networkx.drawing.draw` to produce good looking graphs.
"""
import networkx as nx
import matplotlib.pyplot as plt

fig, axarr = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))

G1 = nx.gn_graph(20)

pos, edges_path = nx.layered_layout(G1, align="vertical")
nx.draw(G1, pos, edges_path=edges_path, ax=axarr[0][0])
axarr[0][0].set_title("Growing Network graph, vertical")

pos, edges_path = nx.layered_layout(G1, align="horizontal")
nx.draw(G1, pos, edges_path=edges_path, ax=axarr[0][1])
axarr[0][1].set_title("Growing Network graph, horizontal")

G2 = nx.binomial_tree(4, create_using=nx.DiGraph)

pos, edges_path = nx.layered_layout(G2, align="vertical")
nx.draw(G2, pos, edges_path=edges_path, ax=axarr[1][0])
axarr[1][0].set_title("Binomial tree graph, vertical")

pos, edges_path = nx.layered_layout(G2, align="horizontal")
nx.draw(G2, pos, edges_path=edges_path, ax=axarr[1][1])
axarr[1][1].set_title("Binomial tree graph, horizontal")

plt.tight_layout()
plt.show()
Loading