Python Networkx:如何更改节点索引

Python Networkx:如何更改节点索引,python,grid,position,coordinates,networkx,Python,Grid,Position,Coordinates,Networkx,我正在使用一个由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

我正在使用一个由
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
中更改什么?

我假设您正在遵循以下示例:

话虽如此,如果你像他们那样做,你的照片就会像他们的一样。如果您只是想让“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)

它起作用了!但是如果你能看看这个问题(),你就会明白我的全部问题。通过应用您的提示,我达到了需要进一步镜像结果的情况,这次是关于y轴的。这意味着我看到的结果是颠倒的。我希望问题中的示例图像能帮助您理解。