source: orange/docs/reference/rst/code/network-optimization-nx.py @ 9372:aef193695ea9

Revision 9372:aef193695ea9, 862 bytes checked in by mitar, 2 years ago (diff)

Moved documentation to the separate directory.

Line 
1import Orange.network
2
3from matplotlib import pyplot as plt
4
5# create graph object of type GraphAsList
6net = Orange.network.Graph()
7net.add_nodes_from(range(5))
8
9# set edges
10for i in range(4):
11    for j in range(i + 1, 5):
12        net.add_edge(i, j)
13
14# vertices are placed randomly in NetworkOptimization constructor
15net_layout = Orange.network.GraphLayout()
16net_layout.set_graph(net)
17
18# optimize verices layout with one of included algorithms
19net_layout.fr(100, 1000)
20
21# read all edges and plot a line
22for u, v in net.edges():
23    x1, y1 = net_layout.coors[0][u], net_layout.coors[1][u]
24    x2, y2 = net_layout.coors[0][v], net_layout.coors[1][v]
25    plt.plot([x1, x2], [y1, y2], 'b-')
26
27# read x and y coordinates to Python list
28x = net_layout.coors[0]
29y = net_layout.coors[1]
30
31# plot vertices
32plt.plot(x, y, 'ro')
33plt.savefig("network-optimization-nx.py.png")
Note: See TracBrowser for help on using the repository browser.