free network in a lattice

I want to generate graph form an adjacency list but I am not happy with how the nodes are positioned. I want them to be positioned according to a pre-defined scheme which resembles a regular grid with arbitrary coordinates x and y, while still maintaining the scale-free features. Let me give an example: A barabasi-albert network with node 1 located at x_1 = 0.6 and y_1 = 0.5, node 2 located at x_2 = -0.5 and y_2 = 1 ... and so on. I have a list of coordinates of each node.


Have a look at the pos parameter of draw_networkx_XXX functions here.

It can be used like this:

 import networkx as nx
 import matplotlib.pyplot as plt
 from random import randint,seed; 

 seed(1)
 nodes = list(range(5))
 edges = [ (nodes[i-1],nodes[i]) for i in range(1,len(nodes)) ]
 # here we can set the coordinates to our liking
 positions = { node:(randint(0,9),randint(0,9)) for node in nodes }
 G=nx.Graph()
 G.add_nodes_from(nodes)
 G.add_edges_from(edges)
 nx.draw_networkx(G,pos=positions, with_labels=False, node_size=100)
 plt.show()

产量

[Edit]

Here's how we can build the graph from the adjancency list and assign real values to node positions.

import networkx as nx
import matplotlib.pyplot as plt
from random import randint,seed 
from pprint import pprint

seed(0)
edges = [ (randint(0,5),randint(0,5)) for i in range(5) ]
G=nx.Graph()
# nodes added automatically with add_edges_from
G.add_edges_from(edges)
# here we can set positions to our liking
positions = { node: (round((5-randint(0,9))/7.0,2)
                    , round((5-randint(0,9))/7.0,2)) for node in G.nodes }
pprint({ "edges:": edges, "nodes:":list(G.nodes), "positions:":positions }, width=100)
nx.draw_networkx(G, pos = positions, with_labels=False, node_size=100)
plt.show()

输出为实际值

Using positions from a csv file is straightforward.
The pos parameter is really supposed to be a dict with node names as keys (I edited the first snippet to reflect that).
So, if we have a csv file with node names and positions, we just build a dict from it and supply the dict for pos .

链接地址: http://www.djcxy.com/p/22734.html

上一篇: 如何对amxn矩阵的所有m行进行排序并对n列进行排序?

下一篇: 网格中的自由网络