Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.drawing.layout.spring_layout

spring_layout(G, k=None, pos=None, fixed=None, iterations=50, weight='weight', scale=1, center=None, dim=2)

Position nodes using Fruchterman-Reingold force-directed algorithm.

Parameters:
  • G (NetworkX graph or list of nodes) – A position will be assigned to every node in G.
  • k (float (default=None)) – Optimal distance between nodes. If None the distance is set to 1/sqrt(n) where n is the number of nodes. Increase this value to move nodes farther apart.
  • pos (dict or None optional (default=None)) – Initial positions for nodes as a dictionary with node as keys and values as a coordinate list or tuple. If None, then use random initial positions.
  • fixed (list or None optional (default=None)) – Nodes to keep fixed at initial position.
  • iterations (int optional (default=50)) – Number of iterations of spring-force relaxation
  • weight (string or None optional (default=’weight’)) – The edge attribute that holds the numerical value used for the edge weight. If None, then all edge weights are 1.
  • scale (number (default: 1)) – Scale factor for positions. Not used unless fixed is None.
  • center (array-like or None) – Coordinate pair around which to center the layout. Not used unless fixed is None.
  • dim (int) – Dimension of layout.
Returns:

pos – A dictionary of positions keyed by node

Return type:

dict

Examples

>>> G = nx.path_graph(4)
>>> pos = nx.spring_layout(G)

# The same using longer but equivalent function name >>> pos = nx.fruchterman_reingold_layout(G)