Python:在二维网格图中反映位置

Python:在二维网格图中反映位置,python,grid,position,networkx,cartesian-coordinates,Python,Grid,Position,Networkx,Cartesian Coordinates,在一个包含10x10个节点的2D图中,我意识到我希望从左上角开始,向下和按列标记节点: 1st column -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2nd column -> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 以此类推,直到我到达第10列。相反,我的代码从左下角开始,向上和按列标记它们我猜警告在pos2行中,但我不知道如何更改它。我曾尝试反转inds和VAL列表,但结果是相对于y轴或纵轴的图形反射。相反,

在一个包含10x10个节点的2D图中,我意识到我希望从左上角开始,向下和按列标记节点:

1st column -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2nd column -> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
以此类推,直到我到达第10列。相反,我的代码从左下角开始,向上和按列标记它们我猜警告在
pos2
行中,但我不知道如何更改它。
我曾尝试
反转
inds
VAL
列表,但结果是相对于y轴或纵轴的图形反射。相反,我在寻找一个关于水平轴的反射

import networkx as nx
from pylab import *
import matplotlib.pyplot as plt
%pylab inline

#n=100 Number of nodes
ncols=10 #Number of columns in a 10x10 grid of positions

N=10 #Nodes per side
G=nx.grid_2d_graph(N,N)
labels = dict( ((i,j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
inds=labels.keys()
vals=labels.values()
inds=sorted(inds,reverse=False)
vals=sorted(vals, reverse=False)
pos2=dict(zip(vals,inds))
nx.draw_networkx(G, pos=pos2, with_labels=True, node_size = 250, node_color='lightblue')
plt.axis('off')
plt.show()

这样绘制图形时,只需更改标签即可

import networkx as nx                                                                                                                        
import matplotlib.pyplot as plt                                                 

#n=100 Number of nodes                                                          
ncols=10 #Number of columns in a 10x10 grid of positions                        

N=10 #Nodes per side                                                            
G=nx.grid_2d_graph(N,N)                                                         
pos = dict(zip(G.nodes(),G.nodes()))                                            
ordering = [(y,N-1-x) for y in range(N) for x in range(N)]                      
labels = dict(zip(ordering, range(len(ordering))))                              
nx.draw_networkx(G, pos=pos, with_labels=False, node_size = 250, node_color='lightblue')                                                                       
nx.draw_networkx_labels(G, pos=pos, labels=labels)                              
plt.axis('off')                                                                 
plt.show()

您可以在这样绘制图形时更改标签

import networkx as nx                                                                                                                        
import matplotlib.pyplot as plt                                                 

#n=100 Number of nodes                                                          
ncols=10 #Number of columns in a 10x10 grid of positions                        

N=10 #Nodes per side                                                            
G=nx.grid_2d_graph(N,N)                                                         
pos = dict(zip(G.nodes(),G.nodes()))                                            
ordering = [(y,N-1-x) for y in range(N) for x in range(N)]                      
labels = dict(zip(ordering, range(len(ordering))))                              
nx.draw_networkx(G, pos=pos, with_labels=False, node_size = 250, node_color='lightblue')                                                                       
nx.draw_networkx_labels(G, pos=pos, labels=labels)                              
plt.axis('off')                                                                 
plt.show()

以下操作对我有效:删除行
vals=sorted(vals,reverse=False)
,并将行
inds=sorted(inds,reverse=False)
替换为
inds=[(N-j-1,N-i-1)对于inds中的i,j]
以下操作对我有效:删除行
vals=sorted(vals,reverse=False)
,并将行
inds=sorted(inds,reverse=False)
替换为
inds=[(N-j-1,N-i-1)表示inds中的i,j]