Drawing a grid of nodes

1,487 views
Skip to first unread message

kamran

unread,
Nov 23, 2011, 5:44:56 PM11/23/11
to networkx-discuss
Hi All,
I am using the following code to draw the gird of nodes(4 by 4)

>>> import matplotlib.pyplot as plt
>>> import networkx as nx
>>> G=nx.grid_2d_graph(4,4)
>>> nx.draw(G);
>>> plt.show();

The code above draws the gird of nodes but produces it in a tilted
way.Also it changes the position of nodes every time I rerun code.How
can i draw nodes having fixed position of nodes and grids every time
i draw it

Regards,
Khalid

Anupama Vasant Patil

unread,
Nov 23, 2011, 6:10:39 PM11/23/11
to networkx...@googlegroups.com
Hi Khalid,

You have to fix the positions .
see the netwrokX tutorial.
it should be nx.draw(G,pos)
where pos is fixed positions of the nodes .

-Anupama Patil


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.




--
-Anupama

Aric Hagberg

unread,
Nov 23, 2011, 6:23:32 PM11/23/11
to networkx...@googlegroups.com
On Wed, Nov 23, 2011 at 4:10 PM, Anupama Vasant Patil
<anupa...@gmail.com> wrote:
> Hi Khalid,
> You have to fix the positions .
> see the netwrokX tutorial.
> it should be nx.draw(G,pos)
> where pos is fixed positions of the nodes .
> -Anupama Patil

For grid_graph() the node labels can be used as positions.
So you can write this:

import networkx as nx
import matplotlib.pyplot as plt
G = nx.grid_2d_graph(10,8)
pos = dict(zip(G,G)) # dictionary of node names->positions
nx.draw(G,pos,with_labels=False)
plt.show

Aric

Khalid chaudhry

unread,
Nov 23, 2011, 6:57:54 PM11/23/11
to networkx...@googlegroups.com
Thanks Aric and Anupama
Aric,

Super cool stuff.The provided code(i changed "with_labels=True") displays nodes labels with (0,0) starting from bottom left.How can i label the nodes so that first node with (0,0) label starts from top left.

Regards,
khalid


Aric Hagberg

unread,
Nov 23, 2011, 7:05:21 PM11/23/11
to networkx...@googlegroups.com
On Wed, Nov 23, 2011 at 4:57 PM, Khalid chaudhry <khali...@gmail.com> wrote:
> Thanks Aric and Anupama
> Aric,
> Super cool stuff.The provided code(i changed "with_labels=True") displays
> nodes labels with (0,0) starting from bottom left.How can i label the nodes
> so that first node with (0,0) label starts from top left.

That is a matplotlib question - you can try adding this line (before show)
plt.setp(plt.gca(), 'ylim', list(reversed(plt.getp(plt.gca(), 'ylim'))))
which is what is suggested here:
http://www.mail-archive.com/matplotl...@lists.sourceforge.net/msg04291.html
Aric

kamran

unread,
Nov 23, 2011, 7:32:14 PM11/23/11
to networkx-discuss
Thanks Aric its working fine.You are a gem

On Nov 23, 4:05 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:


> On Wed, Nov 23, 2011 at 4:57 PM, Khalid chaudhry <khalidch...@gmail.com> wrote:
> > Thanks Aric and Anupama
> > Aric,
> > Super cool stuff.The provided code(i changed "with_labels=True") displays
> > nodes labels with (0,0) starting from bottom left.How can i label the nodes
> > so that first node with (0,0) label starts from top left.
>
> That is a matplotlib question - you can try adding this line (before show)
> plt.setp(plt.gca(), 'ylim', list(reversed(plt.getp(plt.gca(), 'ylim'))))
> which is what is suggested here:

> http://www.mail-archive.com/matplotlib-us...@lists.sourceforge.net/msg04291.html
> Aric

Reply all
Reply to author
Forward
0 new messages