multipartite_layout#

multipartite_layout(G, subset_key='subset', align='vertical', scale=1, center=None, store_pos_as=None)[source]#

Position nodes in layers of straight lines.

Parameters:
GNetworkX graph or list of nodes

A position will be assigned to every node in G.

subset_keystring or dict (default=’subset’)

If a string, the key of node data in G that holds the node subset. If a dict, keyed by layer number to the nodes in that layer/subset.

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.

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.

Notes

This algorithm currently only works in two dimensions and does not try to minimize edge crossings.

Network does not need to be a complete multipartite graph. As long as nodes have subset_key data, they will be placed in the corresponding layers.

Examples

>>> G = nx.complete_multipartite_graph(28, 16, 10)
>>> pos = nx.multipartite_layout(G)
>>> # supress the returned dict and store on the graph directly
>>> G = nx.complete_multipartite_graph(28, 16, 10)
>>> _ = nx.multipartite_layout(G, store_pos_as="pos")

or use a dict to provide the layers of the layout

>>> G = nx.Graph([(0, 1), (1, 2), (1, 3), (3, 4)])
>>> layers = {"a": [0], "b": [1], "c": [2, 3], "d": [4]}
>>> pos = nx.multipartite_layout(G, subset_key=layers)