random_layout#

random_layout(G, center=None, dim=2, seed=None, store_pos_as=None)[source]#

Position nodes uniformly at random in the unit square.

For every node, a position is generated by choosing each of dim coordinates uniformly at random on the interval [0.0, 1.0).

NumPy (http://scipy.org) is required for this function.

Parameters:
GNetworkX graph or list of nodes

A position will be assigned to every node in G.

centerarray-like or None

Coordinate pair around which to center the layout.

dimint

Dimension of layout.

seedint, RandomState instance or None optional (default=None)

Set the random state for deterministic node layouts. If int, seed is the seed used by the random number generator, if numpy.random.RandomState instance, seed is the random number generator, if None, the random number generator is the RandomState instance used by numpy.random.

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

Examples

>>> G = nx.lollipop_graph(4, 3)
>>> pos = nx.random_layout(G)
>>> # supress the returned dict and store on the graph directly
>>> _ = nx.random_layout(G, seed=42, store_pos_as="pos")
>>> nx.get_node_attributes(G, "pos")
{0: array([0.37454012, 0.9507143 ], dtype=float32), 1: array([0.7319939, 0.5986585], dtype=float32), 2: array([0.15601864, 0.15599452], dtype=float32), 3: array([0.05808361, 0.8661761 ], dtype=float32), 4: array([0.601115 , 0.7080726], dtype=float32), 5: array([0.02058449, 0.96990985], dtype=float32), 6: array([0.83244264, 0.21233912], dtype=float32)}