Changing the color of dynamic nodes

1,629 views
Skip to first unread message

naman

unread,
May 1, 2011, 1:40:36 PM5/1/11
to networkx...@googlegroups.com
I have something like the below code to differentiate nodes of blue color and red color

for node in self.state.graph.Graph.keys():                           # this node is a node of my class
                if state.graph.PacketNode.NodeID == node:     # if it has a packet then color it blue
                    G.add_node(node,node_color = 'b')
                    node_blue.append(node)                      # node_blue is a list --------- I think this is wrong because node is of my class and in the examples it shows that of pos.....
                else:
                    node_red.append(node)
                    G.add_node(node,node_color = 'b')
                for value in self.state.graph.Graph[node].keys():
                    G.add_edge( node , value , weight =  state.graph.Graph[node][value] )
networkx.drawing.nx_pylab.draw_networkx(G,pos,ax = self.axes,node_color = 'b',node_list = node_blue)
networkx.drawing.nx_pylab.draw_networkx(G,pos,ax = self.axes,node_color = 'r',node_list = node_red)

Now I know that the node_list contains the networkx node .... But my problem is that these nodes of my class will be dynamic so I want to change the list everytime.....
Is there a way to change the color in this situation of some nodes ?

--
Naman

Aric Hagberg

unread,
May 1, 2011, 3:59:48 PM5/1/11
to networkx...@googlegroups.com

Maybe you could try something like this:

import matplotlib.pyplot as plt
import networkx as nx
G=nx.Graph()
G.add_nodes_from('abc',color='red')
G.add_nodes_from('de',color='blue')
G.add_edges_from([('a','b'),('b','d'),('c','e'),('b','e')])
pos=nx.spring_layout(G)

blue_nodes=[n for n,d in G.nodes(data=True) if d['color']=='blue']
red_nodes=[n for n,d in G.nodes(data=True) if d['color']=='red']
nx.draw_networkx_nodes(G,pos,nodelist=blue_nodes,node_color='b')
nx.draw_networkx_nodes(G,pos,nodelist=red_nodes,node_color='r')
nx.draw_networkx_edges(G,pos)
plt.show()

Aric

naman

unread,
May 1, 2011, 5:39:25 PM5/1/11
to networkx...@googlegroups.com
It works but it hides the labels of the nodes .... Do you have any option for that ?


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




--
Naman

Aric Hagberg

unread,
May 1, 2011, 6:32:18 PM5/1/11
to networkx...@googlegroups.com
On Sun, May 1, 2011 at 3:39 PM, naman <naman...@gmail.com> wrote:
> It works but it hides the labels of the nodes .... Do you have any option
> for that ?

Set the font to a different color? Or maybe change the alpha value of
the nodes?

nx.draw_networkx_nodes(G,pos,nodelist=blue_nodes,node_color='b',alpha=0.7)
nx.draw_networkx_nodes(G,pos,nodelist=red_nodes,node_color='r',alpha=0.7)
nx.draw_networkx_labels(G,pos,font_color='w')

Aric

Reply all
Reply to author
Forward
0 new messages