2

I am working with a regular network of 100x100=10000 nodes. The network is created just like this:

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

I want to have node 0 in the upper left corner, and node 9999 in the lower right. This is why you see a second call to pos: it is an attempt to change node indexing according to my will.

However, I have noticed that after I run the script: pos[0]=(0,99), pos[99]=(99,99), pos[9900]=(0,0), pos[9999]=(99,0). This means that networkx sees the origin of the grid in the bottom left corner and that the farthest position from the origin, (99,99), belongs to the 99th node.

Now, I want to change that so to have my origin in the upper left corner. This means that I want to have: pos[0]=(0,0), pos[99]=(0,99), pos[9900]=(99,0), pos[9999]=(99,99).

What should I change in pos?

FaCoffee
  • 7,609
  • 28
  • 99
  • 174

1 Answers1

1

I'm assuming you are following the example here: Remove rotation effect when drawing a square grid of MxM nodes in networkx using grid_2d_graph

With that being said, your picture will look like theirs if you do it just like they did. If you just want 'pos' to look different you can use:

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)
Community
  • 1
  • 1
James Tobin
  • 3,070
  • 19
  • 35
  • It works! But if you could take a look at this question (http://stackoverflow.com/questions/33826967/matplotlib-how-to-mirror-an-image-with-respect-to-both-axes), you would see my whole problem. By applying your hint, I reach a situation where I need to further mirror the outcome, this time with respect to the y-axis. This means that I see my outcome *upside down*. I hope the exemplicative image in the question will help you understand. – FaCoffee Nov 20 '15 at 18:32