Previous topic

Reading and Writing Networks

Next topic

Community Detection in Graphs

This Page

Network layout optimization


A class for graph layout optimization. Before using any of the layout optimization technique, the class have to be initialized with the set_graph method. Also, do not forget to call set_graph again if the graph structure changes.


Coordinates of all vertices. They are initialized to random positions. You can modify them manually or use one of the optimization algorithms. Usage: coors[0][i], coors[1][i]; 0 for x-axis, 1 for y-axis

set_graph(graph=None, positions=None)

Init graph structure.

  • graph (Orange.netowork.Graph) – Orange network
  • positions (A list of positions (x, y)) – Initial node positions

Network optimization


Random graph layout.

fr(steps, temperature, coolFactor=0, weighted=False)

Fruchterman-Reingold spring layout optimization. Set number of iterations with argument steps, start temperature with temperature (for example: 1000).

fr_radial(center, steps, temperature)

Radial Fruchterman-Reingold spring layout optimization. Set center node with attribute center, number of iterations with argument steps and start temperature with temperature (for example: 1000).


Circular graph layout with original node order.


Circular graph layout with random node order.


Circular graph layout with edge crossing reduction (Michael Baur, Ulrik Brandes).


mds_components(mdsSteps, mdsRefresh, callbackProgress=None, callbackUpdateCanvas=None, torgerson=0, minStressDelta=0, avgLinkage=False, rotationOnly=False, mdsType=0, scalingRatio=0, mdsFromCurrentPos=0)

Position the network components according to similarities among them.

rotate_components(maxSteps=100, minMoment=1e-09, callbackProgress=None, callbackUpdateCanvas=None)

Rotate the network components using a spring model.

Helper methods

get_vertices_in_rect(x1, y1, x2, y2)

Return a list of nodes in the given rectangle.

closest_vertex(x, y)

Return the closest node to given point.

vertex_distances(x, y)

Return distances (a list of (distance, vertex) tuples) of all nodes to the given position.

rotate_vertices(components, phi)

Rotate network components for a given angle.

  • components (list of lists of vertex indices) – list of network components
  • phi (float) – list of component rotation angles (unit: radians)


Network constructor and random layout

In our first example we create a Network object with a simple full graph (K5). Vertices are initially placed randomly. Graph is visualized using pylabs matplotlib.


from matplotlib import pyplot as plt

# vertices are placed randomly in Network constructor
net =

# set edges
for i in range(4):
    for j in range(i + 1, 5):
        net.add_edge(i, j)

net_layout =

# read all edges and plot a line
for u, v in net.edges():
    x1, y1 = net_layout.coors[0][u], net_layout.coors[1][u]
    x2, y2 = net_layout.coors[0][v], net_layout.coors[1][v]
    plt.plot([x1, x2], [y1, y2], 'b-')

# read x and y coordinates to Python list
x = net_layout.coors[0]
y = net_layout.coors[1]

# plot vertices
plt.plot(x, y, 'ro')

Executing the above saves a pylab window with the following graph drawing:


Network layout optimization

This example demonstrates how to optimize network layout using one of the included algorithms.

part of

# vertices are placed randomly in NetworkOptimization constructor
net_layout =

# optimize verices layout with one of included algorithms, 1000)

The result of the above script is a spring force layout optimization: