Note
Click here to download the full example code
Bipartite node layout
By default, nodes are partitioned into two subsets using a two-coloring of the graph. The median heuristic proposed in Eades & Wormald (1994) is used to reduce edge crossings.
import matplotlib.pyplot as plt
from netgraph import Graph
edges = [
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(5, 6)
]
Graph(edges, node_layout='bipartite', node_labels=True)
plt.show()
Out:
/home/paul/src/netgraph/netgraph/_node_layout.py:1026: UserWarning: The graph consistst of multiple components, and hence the partitioning into two subsets/layers is ambiguous!
Use the `subsets` argument to explicitly specify the desired partitioning.
warnings.warn(msg)
The partitions can also be made explicit using the subsets
argument.
In multi-component bipartite graphs, multiple two-colorings are possible,
such that explicit specification of the subsets may be necessary to achieve the desired partitioning of nodes.
import matplotlib.pyplot as plt
from netgraph import Graph
edges = [
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(5, 6)
]
Graph(edges, node_layout='bipartite', node_layout_kwargs=dict(subsets=[(0, 2, 4, 6), (1, 3, 5)]), node_labels=True)
plt.show()
To change the layout from the left-right orientation to a bottom-up orientation, call the layout function directly and swap x and y coordinates of the node positions.
import matplotlib.pyplot as plt
from netgraph import Graph, get_bipartite_layout
edges = [
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(5, 6)
]
node_positions = get_bipartite_layout(edges, subsets=[(0, 2, 4, 6), (1, 3, 5)])
node_positions = {node : (x, y) for node, (y, x) in node_positions.items()}
Graph(edges, node_layout=node_positions, node_labels=True)
plt.show()
Total running time of the script: ( 0 minutes 0.362 seconds)