网格中的自由网络

我想生成一个邻接表的图形,但我不满意节点的位置。 我希望它们按照预定义的方案进行定位,该方案类似于具有任意坐标x和y的规则网格,同时仍保持无标度特征。 举个例子:一个节点1的barabasi-albert网络位于x_1 = 0.6和y_1 = 0.5,节点2位于x_2 = -0.5和y_2 = 1 ...等等。 我有一个每个节点的坐标列表。


在这里查看draw_networkx_XXX函数的pos参数。

它可以像这样使用:

 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()

产量

[编辑]

以下是我们如何从邻接列表构建图并将实际值分配给节点位置的方法。

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()

输出为实际值

使用csv文件中的职位很简单。
pos参数实际上应该是一个以节点名称作为键的字典(我编辑了第一个片段以反映它)。
所以,如果我们有一个包含节点名称和位置的csv文件,我们只需从它构建一个字典并提供pos的字典。

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

上一篇: free network in a lattice

下一篇: Eigenvector Centrality Algorithm/Pseudocode