我想从邻接表中生成图形,但我对节点的位置不满意。我希望它们根据预定义的方案进行定位,该方案类似于具有任意坐标x和y的规则网格,同时仍然保持无尺度特征。让我举一个例子:一个巴拉巴西-阿尔伯特网络,节点1位于x_1 = 0.6,y_1 = 0.5,节点2位于x_2 = -0.5,y_2 =1……诸若此类。我有每个节点的坐标列表。
发布于 2018-03-14 02:08:20
看看draw_networkx_XXX函数here的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提供字典。
https://stackoverflow.com/questions/49262391
复制相似问题