Node Layout

get_fruchterman_reingold_layout(edges[, ...])

'Spring' or Fruchterman-Reingold node layout.

get_sugiyama_layout(edges[, origin, scale, ...])

'Dot' or Sugiyama node layout.

get_radial_tree_layout(edges[, origin, ...])

Radial tree layout.

get_circular_layout(edges[, origin, scale, ...])

Circular node layout.

get_bipartite_layout(edges[, nodes, ...])

Bipartite node layout.

get_multipartite_layout(edges, layers[, ...])

Layered node layout for a multipartite graph.

get_shell_layout(edges, shells[, radii, ...])

Shell layout.

get_community_layout(edges, node_to_community)

Community node layout for modular graphs.

get_geometric_layout(edges, edge_length[, ...])

Node layout for defined edge lengths but unknown node positions.

netgraph.get_fruchterman_reingold_layout(edges, edge_weights=None, k=None, origin=(0, 0), scale=(1, 1), pad_by=0.05, initial_temperature=1.0, total_iterations=50, node_size=0, node_positions=None, fixed_nodes=None, *args, **kwargs)[source]

‘Spring’ or Fruchterman-Reingold node layout.

Uses the Fruchterman-Reingold algorithm [Fruchterman1991] to compute node positions. This algorithm simulates the graph as a physical system, in which nodes repell each other. For connected nodes, this repulsion is counteracted by an attractive force exerted by the edges, which are simulated as springs. The resulting layout is hence often referred to as a ‘spring’ layout.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

edge_weightsdict

Mapping of edges to edge weights.

kfloat or None, default None

Expected mean edge length. If None, initialized to the sqrt(area / total nodes).

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

total_iterationsint, default 50

Number of iterations.

initial_temperature: float, default 1.

Temperature controls the maximum node displacement on each iteration. Temperature is decreased on each iteration to eventually force the algorithm into a particular solution. The size of the initial temperature determines how quickly that happens. Values should be much smaller than the values of scale.

node_sizescalar or dict, default 0.

Size (radius) of nodes. Providing the correct node size minimises the overlap of nodes in the graph, which can otherwise occur if there are many nodes, or if the nodes differ considerably in size.

node_positionsdict or None, default None

Mapping of nodes to their (initial) x,y positions. If None are given, nodes are initially placed randomly within the bounding box defined by origin and scale. If the graph has multiple components, explicit initial positions may result in a ValueError, if the initial positions fall outside of the area allocated to that specific component.

fixed_nodeslist or None, default None

Nodes to keep fixed at their initial positions.

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Fruchterman1991]

Fruchterman, TMJ and Reingold, EM (1991) ‘Graph drawing by force‐directed placement’, Software: Practice and Experience

netgraph.get_sugiyama_layout(edges, origin=(0, 0), scale=(1, 1), pad_by=0.05, node_size=3, total_iterations=3)[source]

‘Dot’ or Sugiyama node layout.

Uses the Sugiyama algorithm [Sugiyama1981] to compute node positions. This function is a wrapper around the SugiyamaLayout class in grandalf.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

total_iterationsint, default 50

Number of iterations.

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Sugiyama1981]

Sugiyama, K; Tagawa, S; Toda, M (1981) ‘Methods for visual understanding of hierarchical system structures’, IEEE Transactions on Systems, Man, and Cybernetics

netgraph.get_radial_tree_layout(edges, origin=(0, 0), scale=(1, 1), pad_by=0.05, node_size=3, total_iterations=3)[source]

Radial tree layout.

Uses the Sugiyama algorithm [Sugiyama1981] to compute node positions. This function is a wrapper around the SugiyamaLayout class in grandalf.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

total_iterationsint, default 50

Number of iterations.

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Sugiyama1981]

Sugiyama, K; Tagawa, S; Toda, M (1981) ‘Methods for visual understanding of hierarchical system structures’, IEEE Transactions on Systems, Man, and Cybernetics

netgraph.get_circular_layout(edges, origin=(0, 0), scale=(1, 1), pad_by=0.05, node_order=None, reduce_edge_crossings=True)[source]

Circular node layout.

By default, this implementation uses a heuristic to arrange the nodes such that the edge crossings are minimised.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

node_orderlist or None, default None

The (initial) ordering of nodes (counter-clockwise) before layout optimisation. Set reduce_edge_crossings to False to skip optimisation and retain the given node order in the returned layout.

reduce_edge_crossingsbool, default True

If True, attempts to minimize edge crossings via the algorithm outlined in [Baur2005].

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Baur2005]

Baur & Brandes (2005) Crossing reduction in circular layouts.

netgraph.get_bipartite_layout(edges, nodes=None, subsets=None, origin=(0, 0), scale=(1, 1), pad_by=0.05, reduce_edge_crossings=True)[source]

Bipartite node layout.

By default, this implementation uses a heuristic to arrange the nodes such that the edge crossings are reduced.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

subsetslist

The two layers of the graph. If None, a two-coloring is used to separate the nodes into two subsets. However, if the graph consists of multiple components, this partitioning into two layers is ambiguous, as multiple solutions exist.

origintuple

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

reduce_edge_crossingsbool, default True

If True, attempts to reduce edge crossings via the algorithm outlined in [Eades1994].

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Eades1994]

Eades & Wormald (1994) Edge crossings in drawings of bipartite graphs.

netgraph.get_multipartite_layout(edges, layers, layer_positions=None, origin=(0, 0), scale=(1, 1), pad_by=0.05, reduce_edge_crossings=True, uniform_node_spacing=True)[source]

Layered node layout for a multipartite graph.

By default, this implementation uses a heuristic to arrange the nodes such that the edge crossings are reduced.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

layerslist

List of node subsets, one for each layer of the graph.

layer_positionslist, default None

A list of x-coordinates, one for each layer. If None provided, layers are placed evenly between origin[0] and origin[0] + scale[0].

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

reduce_edge_crossingsbool, default True

If True, attempts to reduce edge crossings via the algorithm outlined in [Eades1994].

uniform_node_spacingbool, default True

If True, the spacing between nodes is uniform across layers. Otherwise, nodes in each layer are distributed evenly across the full height of the canvas.

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Eades1994]

Eades & Wormald (1994) Edge crossings in drawings of bipartite graphs.

netgraph.get_shell_layout(edges, shells, radii=None, origin=(0, 0), scale=(1, 1), pad_by=0.05, reduce_edge_crossings=True)[source]

Shell layout.

This is a wrapper around get_multipartite_layout that arranges nodes in shells around a center instead of in layers.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

shellslist

List of node subsets, one for each shell of the graph.

radiilist, default None

List of radii, one for each shell. If None, radii are chosen such that the shells are evenly spaced within the bounding box defined by origin and scale.

origintuple

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

reduce_edge_crossingsbool, default True

If True, attempts to reduce edge crossings via the algorithm outlined in [Eades1994].

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

References

[Eades1994]

Eades & Wormald (1994) Edge crossings in drawings of bipartite graphs.

netgraph.get_community_layout(edges, node_to_community, origin=(0, 0), scale=(1, 1), pad_by=0.05)[source]

Community node layout for modular graphs.

This implements the following steps:

  1. Position the communities with respect to each other: Create a new, weighted graph, where each node corresponds to a community, and the weights correspond to the number of edges between communities. Determine the centroid of each community using the FR algorithm, i.e. a spring layout.

  2. Position the nodes within each community: For each community, create a new graph. Find a layout for the subgraph.

  3. Combine positions computed in in 1) and 3).

  4. Rotate communities around their centroid to reduce the length of the edges between them.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

node_to_communitydict

The network partition, which maps each node ID to a community ID.

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box:

xmin = origin[0] + pad_by * scale[0]

xmax = origin[0] + scale[0] - pad_by * scale[0]

ymin = origin[1] + pad_by * scale[1]

ymax = origin[1] + scale[1] - pad_by * scale[1]

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.

netgraph.get_geometric_layout(edges, edge_length, node_size=0.0, tol=0.001, origin=(0, 0), scale=(1, 1), pad_by=0.05)[source]

Node layout for defined edge lengths but unknown node positions.

Node positions are determined through non-linear optimisation: the total distance between nodes is maximised subject to the constraint imposed by the edge lengths, which are used as upper bounds. If provided, node sizes are used to set lower bounds to minimise collisions.

..note:: This implementation is slow.

Parameters:
edgeslist

The edges of the graph, with each edge being represented by a (source node ID, target node ID) tuple.

edge_lengthdict

Mapping of edges to their lengths.

node_sizescalar or dict, default 0.

Size (radius) of nodes. Providing the correct node size minimises the overlap of nodes in the graph, which can otherwise occur if there are many nodes, or if the nodes differ considerably in size.

tolfloat, default 1e-3

The tolerance of the cost function. Small values increase the accuracy, large values improve the computation time.

origintuple, default (0, 0)

The (float x, float y) coordinates corresponding to the lower left hand corner of the bounding box specifying the extent of the canvas.

scaletuple, default (1, 1)

The (float x, float y) dimensions representing the width and height of the bounding box specifying the extent of the canvas.

pad_byfloat, default 0.05

Padding around node positions to reduce clipping of the node artists with the frame, and to create space for routing curved edges including self-loops around nodes. This results in the following bounding box: xmin, xmax = origin[0] + pad_by * scale[0], origin[0] + scale[0] - pad_by * scale[0] ymin, ymax = origin[1] + pad_by * scale[1], origin[1] + scale[1] - pad_by * scale[1]

Returns:
node_positionsdict

Dictionary mapping each node ID to (float x, float y) tuple, the node position.