dimitrisd
dimitrisd

Reputation: 652

How to change the color of a set of nodes?

I have the following graph. I want to set a different node color for avengers and guardians of galaxy but the following doesn't work. Do you have any suggestions?

g = nx.DiGraph()
g.add_nodes_from(guardians,node_color='b')
g.add_edges_from(guardians_links)

g.add_nodes_from(avengers,node_color='y')
g.add_edges_from(avengers_links)

plt.figure(figsize=(12,10))
nx.draw_random(g)
plt.show() # display

Upvotes: 0

Views: 1242

Answers (1)

Joel
Joel

Reputation: 23827

replace nx.draw_random(g) with

pos = nx.random_layout(g)
nx.draw(g,pos=pos, nodelist = guardians, node_color='b')
nx.draw(g,pos=pos, nodelist = avengers, node_color='y')

And you don't really need to be assigning node colors when you add the nodes.

Here are the instructions for nx.draw. The optional keywords are described in the documentation for nx.draw_networkx.

Upvotes: 2

Related Questions