# retworkx.spring_layout¶

spring_layout(graph, pos=None, fixed=None, k=None, repulsive_exponent=2, adaptive_cooling=True, num_iter=50, tol=1e-06, weight_fn=None, default_weight=1, scale=1, center=None, seed=None)[source]
spring_layout(graph: retworkx.PyDiGraph, pos=None, fixed=None, k=None, repulsive_exponent=2, adaptive_cooling=True, num_iter=50, tol=1e-06, weight_fn=None, default_weight=1, scale=1, center=None, seed=None)
spring_layout(graph: retworkx.PyGraph, pos=None, fixed=None, k=None, repulsive_exponent=2, adaptive_cooling=True, num_iter=50, tol=1e-06, weight_fn=None, default_weight=1, scale=1, center=None, seed=None)

Position nodes using Fruchterman-Reingold force-directed algorithm.

The algorithm simulates a force-directed representation of the network treating edges as springs holding nodes close, while treating nodes as repelling objects, sometimes called an anti-gravity force. Simulation continues until the positions are close to an equilibrium.

Parameters
• graph – Graph to be used. Can either be a PyGraph or PyDiGraph.

• pos (dict) – Initial node positions as a dictionary with node ids as keys and values as a coordinate list. If None, then use random initial positions. (default=None)

• fixed (set) – Nodes to keep fixed at initial position. Error raised if fixed specified and pos is not. (default=None)

• k (float) – Optimal distance between nodes. If None the distance is set to $$\frac{1}{\sqrt{n}}$$ where $$n$$ is the number of nodes. Increase this value to move nodes farther apart. (default=None)

• repulsive_exponent (int) – Repulsive force exponent. (default=2)

• adaptive_cooling (bool) – Use an adaptive cooling scheme. If set to False, a linear cooling scheme is used. (default=True)

• num_iter (int) – Maximum number of iterations. (default=50)

• tol (float) – Threshold for relative error in node position changes. The iteration stops if the error is below this threshold. (default = 1e-6)

• weight_fn – An optional weight function for an edge. It will accept a single argument, the edge’s weight object and will return a float which will be used to represent the weight of the edge.

• default_weight (float (default=1)) – If weight_fn isn’t specified this optional float value will be used for the weight/cost of each edge

• scale (float|None) – Scale factor for positions. Not used unless fixed is None. If scale is None, no re-scaling is performed. (default=1.0)

• center (list) – Coordinate pair around which to center the layout. Not used unless fixed is None. (default=None)

• seed (int) – An optional seed to use for the random number generator

Returns

A dictionary of positions keyed by node id.

Return type

dict