change node color at (0,0) position in 100 by 100 grid of nodes

756 views
Skip to first unread message

Khalid

unread,
Nov 24, 2011, 4:05:20 PM11/24/11
to networkx-discuss
Hi all,

I am having the following code to draw a grid of 100 by 100 nodes
>>> import networkx as nx
>>> import matplotlib.pyplot as plt
>>> G = nx.grid_2d_graph(100,100)
>>> pos = dict(zip(G,G)) # dictionary of node names->positions
>>> nx.draw(G,pos,node_size=8,with_labels=False)
>>> plt.show()
I want to change the color of node at (0,0) position to blue or
something else and its related edges.Basically I am using
nx.write_adjlist(G,"test.adjlist") before the draw in above code and
my requirement is to read that file(test.adjlist) and applying blue
color to the node,its adjacent nodes and its related edges based on
the first line read from adjlist and repeat the process for the rest
of the lines in the "test.adjlist" file.

Regards,
Khalid

Thomas Capelle

unread,
Nov 24, 2011, 4:24:14 PM11/24/11
to networkx...@googlegroups.com
You can give a node_list argument to nx.draw() and a node_color argument. With that you can make wathever you want. Use separate nx.draw_networkx_nodes() and draw_networkx_edges() if you want to change one by one.
try something like this:

nx.draw_networkx_nodes(G,pos,nodelist=[n],
node_size=800,
node_color='b',
cmap=plt.cm.Reds_r)

Use the attribute of node_color='b' in the nx.draw() function.

> --
> 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.
>
>
>

Khalid chaudhry

unread,
Nov 24, 2011, 5:34:59 PM11/24/11
to networkx...@googlegroups.com
Hi Thomas,
I am using following code to change the color of a node with label (0,0) 
G = nx.grid_2d_graph(3,3);
pos = dict(zip(G,G)) # dictionary of node names->positions
nx.draw(G,pos,node_size=8,with_labels=True,node_list=[(0,0)],node_color='b');
plt.show()

But the above code changes all of the colors of nodes to blue rather than only the node with the label (0,0).What i am missing?

Regards,
Khalid

Khalid chaudhry

unread,
Nov 24, 2011, 5:40:46 PM11/24/11
to networkx...@googlegroups.com
I have seen example at
http://networkx.lanl.gov/examples/drawing/house_with_colors.html but
its specifying the position explicitly.I don't want to specify the
position explicitly because my grid is of 100 by 100 nodes so it would
be too cumbersome.


Regards,
Khalid

Thomas Capelle

unread,
Nov 24, 2011, 6:19:37 PM11/24/11
to networkx...@googlegroups.com
Use nx.draw_networkx_nodes() instead, and give a nodelist of the nodes you want to color.
You can do:

           nx.draw_networkx_nodes(G,pos,nodelist=[0],
                                   node_size=800,
                                   node_color='b',
                                   cmap=plt.cm.Reds_r)

This will only make node 0 blue.

Aric Hagberg

unread,
Nov 24, 2011, 6:19:59 PM11/24/11
to networkx...@googlegroups.com
On Thu, Nov 24, 2011 at 3:34 PM, Khalid chaudhry <khali...@gmail.com> wrote:
> Hi Thomas,
> I am using following code to change the color of a node with label (0,0)
> G = nx.grid_2d_graph(3,3);
> pos = dict(zip(G,G)) # dictionary of node names->positions
> nx.draw(G,pos,node_size=8,with_labels=True,node_list=[(0,0)],node_color='b');
> plt.show()
> But the above code changes all of the colors of nodes to blue rather than
> only the node with the label (0,0).What i am missing?

The keyword arguments are nodelist and node_color. Not very intuitive
or well-documented but it works:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.grid_2d_graph(10,10)


pos = dict(zip(G,G)) # dictionary of node names->positions

nx.draw(G,pos,with_labels=True)
nx.draw_networkx_nodes(G,pos,nodelist=[(0,0)],node_color='b')
plt.show()


Aric

Khalid chaudhry

unread,
Nov 24, 2011, 7:25:44 PM11/24/11
to networkx...@googlegroups.com
Hi Aric and Thomas,

Thanks 

Aric,
Thanks.1 more question.I am using the following code to store the nodes neighbors information(line no 7) as shown below

1.import networkx as nx
2.import matplotlib.pyplot as plt
3. G = nx.grid_2d_graph(10,10)
4. pos = dict(zip(G,G)) # dictionary of node names->positions
5. nx.draw(G,pos,with_labels=True)
6. nx.draw_networkx_nodes(G,pos,nodelist=[(0,0)],node_color='b')
7. nx.write_adjlist(G,"test.adjlist")
Is it possible to store the node color information along with  the its neighbors(that is either it is red or blue)?.Later i will provide that file to Java program and it may change any node color to blue and providing back to networkX to depict the changes.


Regards,
Khalid





Aric Hagberg

unread,
Nov 24, 2011, 10:35:12 PM11/24/11
to networkx...@googlegroups.com
On Thu, Nov 24, 2011 at 5:25 PM, Khalid chaudhry <khali...@gmail.com> wrote:
> Hi Aric and Thomas,
> Thanks&nbsp;

> Aric,
> Thanks.1 more question.I am using the following code to store the nodes
> neighbors information(line no 7) as shown below
> 1.import networkx as nx
> 2.import matplotlib.pyplot as plt
> 3. G = nx.grid_2d_graph(10,10)
> 4. pos = dict(zip(G,G)) # dictionary of node names->positions
> 5. nx.draw(G,pos,with_labels=True)
> 6. nx.draw_networkx_nodes(G,pos,nodelist=[(0,0)],node_color='b')
> 7. nx.write_adjlist(G,"test.adjlist")
> Is it possible to store the node color information along with &nbsp;the its

> neighbors(that is either it is red or blue)?.Later i will provide that file
> to Java program and it may change any node color to blue and providing back
> to networkX to depict the changes.
>

You can store data with nodes by adding attributes. You can do it
when you add nodes or change by modifying G.node. You'll need to use
some other format than adjlist since that doesn't save node
attributes. Maybe GraphML?
e.g.

In [1]: import sys

In [2]: import networkx as nx

In [3]: G=nx.Graph()

In [4]: G.add_node(1,color='r')

In [5]: G.add_node(2)

In [6]: G.node[2]['color']='b'

In [7]: G.nodes(data=True)
Out[7]: [(1, {'color': 'r'}), (2, {'color': 'b'})]

In [8]: nx.write_graphml(G,sys.stdout)
<?xml version="1.0" encoding="utf-8"?><graphml
xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key attr.name="color" attr.type="string" for="node" id="d0" />
<graph edgedefault="undirected">
<node id="1">
<data key="d0">r</data>
</node>
<node id="2">
<data key="d0">b</data>
</node>
</graph>
</graphml>

Aric

Khalid chaudhry

unread,
Nov 24, 2011, 10:52:52 PM11/24/11
to networkx...@googlegroups.com
The problem is that i have grid of 100 by 100 nodes and i can't
manually add every node and set it color as you specified
(G.add_node(1,color='r')).All i need is some indicator('r' or 'b' is
also fine) that shows the color of the node along with the its
neighbor nodes in one file.Kindly suggest something
Regards,
Khalid

Anupama Vasant Patil

unread,
Nov 25, 2011, 12:11:00 AM11/25/11
to networkx...@googlegroups.com
Khalid,

use for or while loop 
you should be able to color all the hundred nodes easily that way

-Anupama
--
-Anupama

Khalid chaudhry

unread,
Nov 25, 2011, 12:18:24 AM11/25/11
to networkx...@googlegroups.com
Anupama,
That would be last option.I am looking for some built in capability
networkX provides.Later i want to read the file in networkX and
regenerate grid based on the color information provided in the
file.You are using loop for it

franck kalala

unread,
Nov 28, 2011, 2:38:05 PM11/28/11
to networkx...@googlegroups.com
Hi Folk,

I want to set a graph in the following way:

G1=gnm(120,571)
G2=gnm(72,307)

I want to generate a graph G form G1 and G2 such that a node in G1 is connected randomly to 2 different nodes in G2.

How one can sort it out?

Fk.



Dan Schult

unread,
Nov 29, 2011, 9:33:22 AM11/29/11
to networkx...@googlegroups.com
Maybe try the disjoint_union function and then connect manually with random choices.  Remember that disjoint_union shifts the integer nodes so they are unique, so the nodes in G2 are shifted by the number of nodes in G1.

Something like:   (untested)

import random

G=nx.disjoint_union(G1,G2)

g1n=random.choice(G1)
g2ns=random.sample(G2,2)
N=len(G1)

G.add_edges_from(  (g1n,n2+N) for n2 in g2ns )




manju

unread,
Nov 30, 2011, 8:09:54 PM11/30/11
to networkx-discuss
Hi Aric,

I have a similar query.

nx.draw_networkx_nodes(G,pos,nodelist=[(0,1)],node_color='b')
In the above statement- Instead of manually setting the nodelist, I
want to read the nodelist from a file. That file will contain the list
of nodes that should be colored blue.

I tried reading from the file and putting the retrieved value in the
nodelist, but its giving error Node '(0,1)' has no position because
may be I am storing it as a string.
Can you please suggest how to read nodelist values from a file.

Regards,
Manju

On Nov 24, 3:19 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:

Kunjan Sheth

unread,
Dec 4, 2011, 7:09:53 AM12/4/11
to networkx-discuss
Hey Manju,

Did you solve the above mentioned problem? I am also facing the same
problem.
It would be great if you can help me.

Aric Hagberg

unread,
Dec 4, 2011, 9:25:23 AM12/4/11
to networkx...@googlegroups.com
On Sun, Dec 4, 2011 at 5:09 AM, Kunjan Sheth <kunja...@gmail.com> wrote:
> Hey Manju,
>
> Did you solve the above mentioned problem? I am also facing the same
> problem.
> It would be great if you can help me.
>
> On Nov 30, 5:09 pm, manju <manju...@gmail.com> wrote:
>> Hi Aric,
>>
>> I have a similar query.
>>
>> nx.draw_networkx_nodes(G,pos,nodelist=[(0,1)],node_color='b')
>> In the above statement- Instead of manually setting the nodelist, I
>> want to read the nodelist from a file. That file will contain the list
>> of nodes that should be colored blue.
>>
>> I tried reading from the file and putting the retrieved value in the
>> nodelist, but its giving error Node '(0,1)' has no position because
>> may be I am storing it as a string.
>> Can you please suggest how to read nodelist values from a file.

Here is some code that shows how to save a graph with node color
attributes (in several different formats), reload the same graph, and
plot with those colors used for the nodes.

Aric

import matplotlib.pyplot as plt
import networkx as nx
G=nx.Graph()
G.add_nodes_from('abc',color='r')
G.add_nodes_from('de',color='b')
G.add_edges_from([('a','b'),('b','d'),('c','e'),('b','e')])
# graphml
nx.write_graphml(G,'color_test.graphml')
H=nx.read_graphml('color_test.graphml')
# gml
#nx.write_gml(G,'color_test.gml')
#H=nx.read_gml('color_test.gml')
# pajek
#nx.write_pajek(G,'color_test.net')
#H=nx.read_pajek('color_test.net')
# gexf
#nx.write_gexf(G,'color_test.gexf')
#H=nx.read_gexf('color_test.gexf')
# json
#import json
#from networkx.readwrite import json_graph
#output = json_graph.adjacency_data(G)
#json.dump(output,open('color_test.json','w'))
#input = json.load(open('color_test.json'))
#H=json_graph.adjacency_graph(input)

# get nodes and colors as lists from graph attributes
nodes,colors=zip(*nx.get_node_attributes(H,'color').items())
nx.draw(H,nodelist=nodes,node_color=colors)
plt.show()

Reply all
Reply to author
Forward
0 new messages