bipartite_layout#
- bipartite_layout(G, nodes=None, align='vertical', scale=1, center=None, aspect_ratio=1.3333333333333333, store_pos_as=None)[source]#
Position nodes in two straight lines.
- Parameters:
- GNetworkX graph or list of nodes
A position will be assigned to every node in G.
- nodescollection of nodes
Nodes in one node set of the graph. This set will be placed on left or top. If
None
(the default), a node set is chosen arbitrarily if the graph if bipartite.- alignstring (default=’vertical’)
The alignment of nodes. Vertical or horizontal.
- scalenumber (default: 1)
Scale factor for positions.
- centerarray-like or None
Coordinate pair around which to center the layout.
- aspect_rationumber (default=4/3):
The ratio of the width to the height of the layout.
- store_pos_asstr, default None
If non-None, the position of each node will be stored on the graph as an attribute with this string as its name, which can be accessed with
G.nodes[...][store_pos_as]
. The function still returns the dictionary.
- Returns:
- posdict
A dictionary of positions keyed by node.
- Raises:
- NetworkXError
If
nodes=None
andG
is not bipartite.
Notes
This algorithm currently only works in two dimensions and does not try to minimize edge crossings.
Examples
>>> G = nx.complete_bipartite_graph(3, 3) >>> pos = nx.bipartite_layout(G)
The ordering of the layout (i.e. which nodes appear on the left/top) can be specified with the
nodes
parameter:>>> top, bottom = nx.bipartite.sets(G) >>> pos = nx.bipartite_layout(G, nodes=bottom) # "bottom" set appears on the left
store_pos_as
can be used to store the node positions for the computed layout directly on the nodes:>>> _ = nx.bipartite_layout(G, nodes=bottom, store_pos_as="pos") >>> nx.get_node_attributes(G, "pos") {0: array([ 1. , -0.75]), 1: array([1., 0.]), 2: array([1. , 0.75]), 3: array([-1. , -0.75]), 4: array([-1., 0.]), 5: array([-1. , 0.75])}
The
bipartite_layout
function can be used with non-bipartite graphs by explicitly specifying how the layout should be partitioned withnodes
:>>> G = nx.complete_graph(5) # Non-bipartite >>> pos = nx.bipartite_layout(G, nodes={0, 1, 2})