我正在使用 100x100=10000 节点的常规网络。网络是这样创建的:
import networkx as nx
import matplotlib.pyplot as plt
N=100
G=nx.grid_2d_graph(N,N) #2D regular graph of 10000 nodes
pos = dict( (n, n) for n in G.nodes() ) #Dict of positions
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
pos = {y:x for x,y in labels.iteritems()} #An attempt to change node indexing
我想在左上角有节点0,在右下角有节点9999。这就是为什么您会看到对 pos 的第二次调用:这是根据我的意愿更改节点索引的尝试。
但是,我注意到在运行脚本后: pos[0]=(0,99), pos[99]=(99,99), pos[9900]=(0,0),pos[9999]=(99,0)。 这意味着 networkx 看到左下角的网格原点,距离原点最远的位置 (99,99) 属于第 99 个节点。
现在,我想改变它,让我的原点在左上角。这意味着我想要: pos[0]=(0,0), pos[99]=(0,99), pos[9900]=(99,0), pos[9999]=(99,99).
我应该在 pos 中更改什么?
请您参考如下方法:
我假设您正在按照此处的示例进行操作:Remove rotation effect when drawing a square grid of MxM nodes in networkx using grid_2d_graph
话虽如此,如果您像他们那样做,您的照片就会看起来像他们的。如果您只是想让“pos”看起来不同,您可以使用:
inds = labels.keys()
vals = labels.values()
inds.sort()
vals.sort()
pos2 = dict(zip(vals,inds))
In [42]: pos2[0]
Out[42]: (0, 0)
In [43]: pos2[99]
Out[43]: (0, 99)
In [44]: pos2[9900]
Out[44]: (99, 0)
In [45]: pos2[9999]
Out[45]: (99, 99)
