Directed Graphs, Multigraphs and Visualization in Networkx
Last Updated :
28 Dec, 2022
Prerequisite:
Basic visualization technique for a Graph
In the
previous article, we have learned about the basics of Networkx module and how to create an undirected graph. Note that Networkx module easily outputs the various Graph parameters easily, as shown below with an example.
Python3 1==
import networkx as nx
edges = [(1, 2), (1, 6), (2, 3), (2, 4), (2, 6),
(3, 4), (3, 5), (4, 8), (4, 9), (6, 7)]
G.add_edges_from(edges)
nx.draw_networkx(G, with_label = True)
print("Total number of nodes: ", int(G.number_of_nodes()))
print("Total number of edges: ", int(G.number_of_edges()))
print("List of all nodes: ", list(G.nodes()))
print("List of all edges: ", list(G.edges(data = True)))
print("Degree for all nodes: ", dict(G.degree()))
print("Total number of self-loops: ", int(G.number_of_selfloops()))
print("List of all nodes with self-loops: ",
list(G.nodes_with_selfloops()))
print("List of all nodes we can go to in a single step from node 2: ",
list(G.neighbors(2)))
Output:
Total number of nodes: 9
Total number of edges: 10
List of all nodes: [1, 2, 3, 4, 5, 6, 7, 8, 9]
List of all edges: [(1, 2, {}), (1, 6, {}), (2, 3, {}), (2, 4, {}), (2, 6, {}), (3, 4, {}), (3, 5, {}), (4, 8, {}), (4, 9, {}), (6, 7, {})]
Degree for all nodes: {1: 2, 2: 4, 3: 3, 4: 4, 5: 1, 6: 3, 7: 1, 8: 1, 9: 1}
Total number of self-loops: 0
List of all nodes with self-loops: []
List of all nodes we can go to in a single step from node 2: [1, 3, 4, 6]
Creating Weighted undirected Graph -
Add list of all edges along with assorted weights -
Python3
import networkx as nx
G = nx.Graph()
edges = [(1, 2, 19), (1, 6, 15), (2, 3, 6), (2, 4, 10),
(2, 6, 22), (3, 4, 51), (3, 5, 14), (4, 8, 20),
(4, 9, 42), (6, 7, 30)]
G.add_weighted_edges_from(edges)
nx.draw_networkx(G, with_labels = True)
We can add the edges via an Edge List, which needs to be saved in a
.txt
format (eg. edge_list.txt)
Python3
G = nx.read_edgelist('edge_list.txt', data =[('Weight', int)])
1 2 19
1 6 15
2 3 6
2 4 10
2 6 22
3 4 51
3 5 14
4 8 20
4 9 42
6 7 30
Edge list can also be read via a Pandas Dataframe -
Python3
import pandas as pd
df = pd.read_csv('edge_list.txt', delim_whitespace = True,
header = None, names =['n1', 'n2', 'weight'])
G = nx.from_pandas_dataframe(df, 'n1', 'n2', edge_attr ='weight')
# The Graph diagram does not show the edge weights.
# However, we can get the weights by printing all the
# edges along with the weights by the command below
print(list(G.edges(data = True)))
Output:
[(1, 2, {'weight': 19}),
(1, 6, {'weight': 15}),
(2, 3, {'weight': 6}),
(2, 4, {'weight': 10}),
(2, 6, {'weight': 22}),
(3, 4, {'weight': 51}),
(3, 5, {'weight': 14}),
(4, 8, {'weight': 20}),
(4, 9, {'weight': 42}),
(6, 7, {'weight': 30})]
We would now explore the different visualization techniques of a Graph.
For this, We've created a Dataset of various Indian cities and the distances between them and saved it in a
.txt
file,
edge_list.txt
.
Kolkata Mumbai 2031
Mumbai Pune 155
Mumbai Goa 571
Kolkata Delhi 1492
Kolkata Bhubaneshwar 444
Mumbai Delhi 1424
Delhi Chandigarh 243
Delhi Surat 1208
Kolkata Hyderabad 1495
Hyderabad Chennai 626
Chennai Thiruvananthapuram 773
Thiruvananthapuram Hyderabad 1299
Kolkata Varanasi 679
Delhi Varanasi 821
Mumbai Bangalore 984
Chennai Bangalore 347
Hyderabad Bangalore 575
Kolkata Guwahati 1031
Now, we will make a Graph by the following code. We will also add a node attribute to all the cities which will be the population of each city.
Python3 1==
import networkx as nx
G = nx.read_weighted_edgelist('edge_list.txt', delimiter =" ")
population = {
'Kolkata' : 4486679,
'Delhi' : 11007835,
'Mumbai' : 12442373,
'Guwahati' : 957352,
'Bangalore' : 8436675,
'Pune' : 3124458,
'Hyderabad' : 6809970,
'Chennai' : 4681087,
'Thiruvananthapuram' : 460468,
'Bhubaneshwar' : 837737,
'Varanasi' : 1198491,
'Surat' : 4467797,
'Goa' : 40017,
'Chandigarh' : 961587
}
# We have to set the population attribute for each of the 14 nodes
for i in list(G.nodes()):
G.nodes[i]['population'] = population[i]
nx.draw_networkx(G, with_label = True)
# This line allows us to visualize the Graph
Output:

But, we can customize the Network to provide more information visually by following these steps:
- The size of the node is proportional to the population of the city.
- The intensity of colour of the node is directly proportional to the degree of the node.
- The width of the edge is directly proportional to the weight of the edge, in this case, the distance between the cities.
Python3 1==
# fixing the size of the figure
plt.figure(figsize =(10, 7))
node_color = [G.degree(v) for v in G]
# node colour is a list of degrees of nodes
node_size = [0.0005 * nx.get_node_attributes(G, 'population')[v] for v in G]
# size of node is a list of population of cities
edge_width = [0.0015 * G[u][v]['weight'] for u, v in G.edges()]
# width of edge is a list of weight of edges
nx.draw_networkx(G, node_size = node_size,
node_color = node_color, alpha = 0.7,
with_labels = True, width = edge_width,
edge_color ='.4', cmap = plt.cm.Blues)
plt.axis('off')
plt.tight_layout();
Output:

We can see in the above code, we have specified the layout type as tight. You can find the different layout techniques and try a few of them as shown in the code below:
Python3 1==
print("The various layout options are:")
print([x for x in nx.__dir__() if x.endswith('_layout')])
# prints out list of all different layout options
node_color = [G.degree(v) for v in G]
node_size = [0.0005 * nx.get_node_attributes(G, 'population')[v] for v in G]
edge_width = [0.0015 * G[u][v]['weight'] for u, v in G.edges()]
plt.figure(figsize =(10, 9))
pos = nx.random_layout(G)
print("Random Layout:")
# demonstrating random layout
nx.draw_networkx(G, pos, node_size = node_size,
node_color = node_color, alpha = 0.7,
with_labels = True, width = edge_width,
edge_color ='.4', cmap = plt.cm.Blues)
plt.figure(figsize =(10, 9))
pos = nx.circular_layout(G)
print("Circular Layout")
# demonstrating circular layout
nx.draw_networkx(G, pos, node_size = node_size,
node_color = node_color, alpha = 0.7,
with_labels = True, width = edge_width,
edge_color ='.4', cmap = plt.cm.Blues)
Output:
The various layout options are:
['rescale_layout',
'random_layout',
'shell_layout',
'fruchterman_reingold_layout',
'spectral_layout',
'kamada_kawai_layout',
'spring_layout',
'circular_layout']
Random Layout:
Circular Layout:
Networkx allows us to create a Path Graph, i.e. a straight line connecting a number of nodes in the following manner:
Python3
G2 = nx.path_graph(5)
nx.draw_networkx(G2, with_labels = True)

We can rename the nodes -
Python3
G2 = nx.path_graph(5)
new = {0:"Germany", 1:"Austria", 2:"France", 3:"Poland", 4:"Italy"}
G2 = nx.relabel_nodes(G2, new)
nx.draw_networkx(G2, with_labels = True)
Creating Directed Graph -
Networkx allows us to work with Directed Graphs. Their creation, adding of nodes, edges etc. are exactly similar to that of an undirected graph as discussed
here.
The following code shows the basic operations on a Directed graph.
Python3 1==
import networkx as nx
G = nx.DiGraph()
G.add_edges_from([(1, 1), (1, 7), (2, 1), (2, 2), (2, 3),
(2, 6), (3, 5), (4, 3), (5, 4), (5, 8),
(5, 9), (6, 4), (7, 2), (7, 6), (8, 7)])
plt.figure(figsize =(9, 9))
nx.draw_networkx(G, with_label = True, node_color ='green')
# getting different graph attributes
print("Total number of nodes: ", int(G.number_of_nodes()))
print("Total number of edges: ", int(G.number_of_edges()))
print("List of all nodes: ", list(G.nodes()))
print("List of all edges: ", list(G.edges()))
print("In-degree for all nodes: ", dict(G.in_degree()))
print("Out degree for all nodes: ", dict(G.out_degree))
print("Total number of self-loops: ", int(G.number_of_selfloops()))
print("List of all nodes with self-loops: ",
list(G.nodes_with_selfloops()))
print("List of all nodes we can go to in a single step from node 2: ",
list(G.successors(2)))
print("List of all nodes from which we can go to node 2 in a single step: ",
list(G.predecessors(2)))
Output:
Total number of nodes: 9
Total number of edges: 15
List of all nodes: [1, 2, 3, 4, 5, 6, 7, 8, 9]
List of all edges: [(1, 1), (1, 7), (2, 1), (2, 2), (2, 3), (2, 6), (3, 5), (4, 3), (5, 8), (5, 9), (5, 4), (6, 4), (7, 2), (7, 6), (8, 7)]
In-degree for all nodes: {1: 2, 2: 2, 3: 2, 4: 2, 5: 1, 6: 2, 7: 2, 8: 1, 9: 1}
Out degree for all nodes: {1: 2, 2: 4, 3: 1, 4: 1, 5: 3, 6: 1, 7: 2, 8: 1, 9: 0}
Total number of self-loops: 2
List of all nodes with self-loops: [1, 2]
List of all nodes we can go to in a single step from node 2: [1, 2, 3, 6]
List of all nodes from which we can go to node 2 in a single step: [2, 7]
Now, we will show the basic operations for a MultiGraph. Networkx allows us to create both directed and undirected Multigraphs. A Multigraph is a Graph where multiple parallel edges can connect the same nodes.
For example, let us create a network of 10 people, A, B, C, D, E, F, G, H, I and J. They have four different relations among them namely Friend, Co-worker, Family and Neighbour. A relation between two people isn't restricted to a single kind.
But the visualization of Multigraph in Networkx is not clear. It fails to show multiple edges separately and these edges overlap.
Python3 1==
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiGraph()
relations = [('A', 'B', 'neighbour'), ('A', 'B', 'friend'), ('B', 'C', 'coworker'),
('C', 'F', 'coworker'), ('C', 'F', 'friend'), ('F', 'G', 'coworker'),
('F', 'G', 'family'), ('C', 'E', 'friend'), ('E', 'D', 'family'),
('E', 'I', 'coworker'), ('E', 'I', 'neighbour'), ('I', 'J', 'coworker'),
('E', 'J', 'friend'), ('E', 'H', 'coworker')]
for i in relations:
G.add_edge(i[0], i[1], relation = i[2])
plt.figure(figsize =(9, 9))
nx.draw_networkx(G, with_label = True)
# getting various graph properties
print("Total number of nodes: ", int(G.number_of_nodes()))
print("Total number of edges: ", int(G.number_of_edges()))
print("List of all nodes: ", list(G.nodes()))
print("List of all edges: ", list(G.edges(data = True)))
print("Degree for all nodes: ", dict(G.degree()))
print("Total number of self-loops: ", int(G.number_of_selfloops()))
print("List of all nodes with self-loops: ", list(G.nodes_with_selfloops()))
print("List of all nodes we can go to in a single step from node E: ",
list(G.neighbors('E")))
Output:
Total number of nodes: 10
Total number of edges: 14
List of all nodes: ['E', 'I', 'D', 'B', 'C', 'F', 'H', 'A', 'J', 'G']
List of all edges: [('E', 'I', {'relation': 'coworker'}), ('E', 'I', {'relation': 'neighbour'}), ('E', 'H', {'relation': 'coworker'}), ('E', 'J', {'relation': 'friend'}), ('E', 'C', {'relation': 'friend'}), ('E', 'D', {'relation': 'family'}), ('I', 'J', {'relation': 'coworker'}), ('B', 'A', {'relation': 'neighbour'}), ('B', 'A', {'relation': 'friend'}), ('B', 'C', {'relation': 'coworker'}), ('C', 'F', {'relation': 'coworker'}), ('C', 'F', {'relation': 'friend'}), ('F', 'G', {'relation': 'coworker'}), ('F', 'G', {'relation': 'family'})]
Degree for all nodes: {'E': 6, 'I': 3, 'B': 3, 'D': 1, 'F': 4, 'A': 2, 'G': 2, 'H': 1, 'J': 2, 'C': 4}
Total number of self-loops: 0
List of all nodes with self-loops: []
List of all nodes we can go to in a single step from node E: ['I', 'H', 'J', 'C', 'D']
Similarly, a Multi Directed Graph can be created by using
Python3 1==
Similar Reads
NetworkX : Python software package for study of complex networks
NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks.
3 min read
Python | Visualize graphs generated in NetworkX using Matplotlib
Prerequisites: Generating Graph using Network X, Matplotlib IntroIn this article, we will be discussing how to plot a graph generated by NetworkX in Python using Matplotlib. NetworkX is not a graph visualizing package but basic drawing with Matplotlib is included in the software package. Step 1 : Im
3 min read
Directed Graphs, Multigraphs and Visualization in Networkx
Prerequisite: Basic visualization technique for a Graph In the previous article, we have learned about the basics of Networkx module and how to create an undirected graph. Note that Networkx module easily outputs the various Graph parameters easily, as shown below with an example. Python3 1== import
10 min read
Complete Graph using Networkx in Python
A complete graph also called a Full Graph it is a graph that has n vertices where the degree of each vertex is n-1. In other words, each vertex is connected with every other vertex. Example: Complete Graph with 6 edges: C_G6 Properties of Complete Graph: The degree of each vertex is n-1.The total nu
3 min read
Introduction to Social Networks using NetworkX in Python
Prerequisite - Python Basics Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social m
4 min read
Wheel Graph using Networkx Python
A wheel graph is a type of graph in which if we connect each node in an n-1 node cycle graph to nth node kept at the centre we get a wheel graph. The definition would be more clear after seeing the example below. Wheel Graph with n nodes is represented by Wn . Example: W5: W5 Â W6: W6 Properties of W
2 min read
Small World Model - Using Python Networkx
In this article, we will learn how to create a Small World Network using Networx module in Python. Before proceeding that let's first understand some basics about Small World Phenomenon. What is Small World Phenomenon ? Small World Phenomenon is the study and notion that we are all connected via a s
4 min read
Ego graph Using Networkx in Python
Prerequisite - Graphs, Networkx Basics Ego network is a special type of network consisting of one central node and all other nodes directly connected to it. The central node is known as ego, while the other surrounding nodes directly connected to it are known as alters. Ego networks are mostly used
4 min read
Star Graph using Networkx Python
In this article, we are going to see Star Graph using Networkx Python. A Star graph is a special type of graph in which n-1 vertices have degree 1 and a single vertex have degree n â 1. This looks like that n â 1 vertex is connected to a single central vertex. A star graph with total n â vertex is t
2 min read
Network Centrality Measures in a Graph using Networkx | Python
Centrality Measures allows us to pinpoint the most important nodes of a Graph. This essentially helps us to identify : Influential nodes in a Social Network. Nodes that disseminate information to many nodes Hubs in a transportation network Important pages in the Web Nodes that prevent the Network fr
6 min read