Change color of nodes when in a spring layout+ networkx
How can I change the nodes of a color where the location is not known
So i have seen this link on stackoverflow where the answer has been given for an explicitly set positions of nodes.
But say i have a graph like this
G = nx.Graph()
for i in range(10):
G.add_node(i)
for i in range(9):
G.add_edge(i,i+1)
po开发者_StackOverflow社区s = nx.spring_layout(G)
node_num = random.randint(0,10)
Now however if I have something as get a random number and color it blue and let others be red .... how would i implement it using nodes_list
attribute of 'draw_networkx'
function
This has nodes for integers. If you can give a solution for any type of node name it would be more helpful....
thanks a lot..
Here is some code that might help. This choses a random node to color blue and colors the rest red.
import random
import matplotlib.pyplot as plt
import networkx as nx
G=nx.Graph()
G.add_edges_from([('a','b'),('b','d'),('c','e'),('b','e')])
pos=nx.spring_layout(G)
nodes=G.nodes()
random.shuffle(nodes)
blue=nodes.pop()
nx.draw_networkx_nodes(G,pos,nodelist=[blue],node_color='b')
nx.draw_networkx_nodes(G,pos,nodelist=nodes,node_color='r')
nx.draw_networkx_edges(G,pos)
plt.show()
精彩评论