spring_layout(G, k=None, pos=None, fixed=None, iterations=50, threshold=0.0001, weight='weight', scale=1, center=None, dim=2, seed=None)
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.
There are some hard-coded values: minimal distance between nodes (0.01) and "temperature" of 0.1 to ensure nodes don't fly away. During the simulation, k
helps determine the distance between nodes, though :None:None:`scale`
and center
determine the size and place after rescaling occurs at the end of the simulation.
Fixing some nodes doesn't allow them to move in the simulation. It also turns off the rescaling feature at the simulation's end. In addition, setting :None:None:`scale`
to :None:None:`None`
turns off rescaling.
A position will be assigned to every node in G.
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.
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.
Nodes to keep fixed at initial position. Nodes not in G.nodes
are ignored. ValueError raised if :None:None:`fixed`
specified and :None:None:`pos`
not.
Maximum number of iterations taken
Threshold for relative error in node position changes. The iteration stops if the error is below this threshold.
The edge attribute that holds the numerical value used for the edge weight. Larger means a stronger attractive force. If None, then all edge weights are 1.
Scale factor for positions. Not used unless :None:None:`fixed is None`
. If scale is None, no rescaling is performed.
Coordinate pair around which to center the layout. Not used unless :None:None:`fixed is None`
.
Dimension of layout.
Set the random state for deterministic node layouts. If int, :None:None:`seed`
is the seed used by the random number generator, if numpy.random.RandomState instance, :None:None:`seed`
is the random number generator, if None, the random number generator is the RandomState instance used by numpy.random.
A dictionary of positions keyed by node
Position nodes using Fruchterman-Reingold force-directed algorithm.
>>> G = nx.path_graph(4)
... pos = nx.spring_layout(G)
# The same using longer but equivalent function name >>> pos = nx.fruchterman_reingold_layout(G)
See :The following pages refer to to this document either explicitly or contain code examples using this.
networkx.drawing.nx_pylab.draw_networkx
networkx.drawing.nx_pylab.draw
networkx.drawing.layout.spring_layout
networkx.drawing.nx_pylab.draw_networkx_labels
networkx.drawing.nx_pylab.draw_networkx_edge_labels
networkx.drawing.nx_pylab.draw_networkx_edges
networkx.drawing.nx_pylab.draw_spring
networkx.drawing.nx_pylab.draw_networkx_nodes
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them