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:

This example draws a colored node graph in Python with NetworkX and Matplotlib. NetworkX builds and analyzes graphs, and a color map assigns a color to each node before drawing.

Visualizing a graph helps you see structure such as clusters and hubs. NetworkX also offers many algorithms for paths, centrality, and connectivity on the same graph object.

Comments

Popular posts from this blog

[Solved] Error: No such keg: /usr/local/Cellar/gcc

[How To] Unfollow Non-followers on Instagram