Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set colors for nodes in NetworkX?

I created my graph, everything looks great so far, but I want to update color of my nodes after creation.

My goal is to visualize DFS, I will first show the initial graph and then color nodes step by step as DFS solves the problem.

If anyone is interested, sample code is available on Github

like image 216
Gokhan Arik Avatar asked Nov 20 '14 02:11

Gokhan Arik


People also ask

What are color nodes?

The Color node is a basic node that lets you change the color of your objects at any time in a non-destructive way. It only outputs color and does not have any inputs. Default Color. Color set to brown. Color set to pink with.

How do I change the size of nodes in NetworkX?

Altering node size globally is, again, quite simple via a keyword argument in the . draw() method — just specify node_size!


1 Answers

All you need is to specify a color map which maps a color to each node and send it to nx.draw function. To clarify, for a 20 node I want to color the first 10 in blue and the rest in green. The code will be as follows:

G = nx.erdos_renyi_graph(20, 0.1) color_map = [] for node in G:     if node < 10:         color_map.append('blue')     else:          color_map.append('green')       nx.draw(G, node_color=color_map, with_labels=True) plt.show() 

You will find the graph in the attached imageenter image description here.

like image 172
Abdallah Sobehy Avatar answered Sep 30 '22 17:09

Abdallah Sobehy