Thursday, September 14, 2017

Plot colored node graph with networkx in Python

Networkx:

NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. URL: https://networkx.github.io/


Code:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.Graph()
color_map = ['blue','green','red']

G.add_nodes_from([1,10])
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(4,3)
G.add_edge(5,3)
G.add_edge(6,8)
G.add_edge(7,9)
G.add_edge(8,7)
G.add_edge(9,6)
G.add_edge(10,5)
G.add_edge(1,4)

nx.draw(G,node_color = color_map,with_labels = True)
plt.show()


Output: