Removing the rotation effect when drawing a square grid of MxM nodes in a network using grid_2d_graph

I need to create a regular graph (also known as a lattice grid) that has 100x100nodes. I started by drawing a graph 10x10with the following code:

import numpy
from numpy import *
import networkx as nx
from networkx import *
import matplotlib.pyplot as plt

G=nx.grid_2d_graph(10,10)        
nx.draw(G)

plt.axis('off')
plt.show()

but I get the following:

enter image description here

Is there a way to get rid of this kind of rotation effects that has an outlet? My final network should look like a chess table, like this (please ignore the tables):

enter image description here

In addition, I need to specify each node its identifier from 0 to 9999 (in the case of a 100x100 network). Any idea would be much appreciated!

+4
2

networkx.draw spring. pos. , , networkx.grid_2d_graph, (, ):

>>> G=nx.grid_2d_graph(2,2)
[(0, 1), (1, 0), (0, 0), (1, 1)]

, node . .

pos = dict( (n, n) for n in G.nodes() )

, node, networkx.draw_networkx, . , . NetworkX node (, ) , node * 10 +:

labels = dict( ((i, j), i * 10 + j) for i, j in G.nodes() )

, , :

import networkx as nx
import matplotlib.pyplot as plt

N = 10
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i * 10 + j) for i, j in G.nodes() )
nx.draw_networkx(G, pos=pos, labels=labels)

plt.axis('off')
plt.show()

plot

@AbdallahSobehy, .

labels = dict( ((i, j), i + (N-1-j) * 10 ) for i, j in G.nodes() )

better marked plot

+7

@mdml ( @mdml)

1- Node nx.grid_2d_graph

, , , Node (i, j), . Node (0,0) → G [(0,0)]

2- ,

, , , :

labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() ) 

, N 10, , N, , . , , node.

3-

Node → G [(0,0)] Node 90 ( , ), G [(1,0)] - Node (91), G [(0,1)] - Node (80), , .

4- , ,

, id Node, , :

for (i,j) in labels:
    G.node[(i,j)]['id'] = labels[(i,j)]

N = 2, 2 3, Id :

for i in xrange(N):
    for j in xrange(N):
        print 'Node ID at: (%d, %d) = %d'  %(i,j,G.node[(i,j)]['id'])
plt.axis('off')
plt.show()

:

Node ID at: (0, 0) = 2
Node ID at: (0, 1) = 0
Node ID at: (1, 0) = 3
Node ID at: (1, 1) = 1

enter image description here

+3

Source: https://habr.com/ru/post/1611920/


All Articles