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
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.
>
>
>
Regards,
Khalid
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
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
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:
Did you solve the above mentioned problem? I am also facing the same
problem.
It would be great if you can help me.
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()