Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Create Interactive Network Graphs in Python

  • Isha BansalIsha Bansal
  • June 22, 2022
  • Python Modules
Create Interactive Network Graphs in Python

I am sure you have built network graphs in python before using a special library known as Networkx. Have you ever wondered if there was a way to interact with graphs? Guess what?! There is a library named Pyvis which helps to improve the interactivity of network graphs in Python programming language.

Also Read: NetworkX Package – Python Graph Library

The Pyvis library enables visualization and adds interactivity to network graphs. The library is built on top of the powerful and mature library known as VisJS JavaScript. This allows fast and responsive interactions and extracts the network graphs in the form of low-level JavaScript and HTML.

Installing the Pyvis library is simple and straightforward that can be done using the pip command in the command prompt of the system using the command below.


Code Implementation

Let’s now move on to the code implementation of the interactive network graphs using the Pyvis library in Python programming language. We will start off by importing all the necessary libraries/modules using the code snippet below.

from pyvis import network as net
from IPython.core.display import display, HTML
import random

We will start off by creating a network graph with only nodes and no edges between them. The creation of an empty graph can be done using the Network function which specifies the properties of the network graph inside it including the background color, heading, height, and width.

Next, we will make use of the add_node function to add nodes to the network graph. We will be adding 10 nodes (from 1 to 10) and then convert the network graph into HTML format to add interactivity and save the HTML file as well.

g_only_nodes =  net.Network(height='600px',width='90%',
                  bgcolor='white',font_color="red",
                  heading="Networkx Graph with only Nodes")

for i in range(1,11):  
  g_only_nodes.add_node(i)

g_only_nodes.show('Only_Nodes.html')
display(HTML('Only_Nodes.html'))

Have a look at what the network graph with only nodes looks like.

The next step in the creation of network graphs is adding edges between the nodes. We will be adding random edges between random nodes. Have a look at the function for the same below.

def generate_edge():
  s = random.randint(1,10)
  d = random.randint(1,10)
  return (s,d)

In the function, we will be generating random source and destination nodes pair using the random.randint function. We will be getting random nodes between 1 and 10. To make sure we have enough edges; we will be generating 20 random edges. To make sure that the same edge is not repeated again and again, we will be keeping a record of the pairs of (source, destination) nodes. Have a look at the code below.

g =  net.Network(height='600px',width='90%',
                  bgcolor='white',font_color="red",
                  heading="A Simple Networkx Graph")

for i in range(1,11):  
  g.add_node(i)

i=0
chosen_set = []
while(i!=20):
  eg = generate_edge()
  if(eg[0]!=eg[1] and not (eg in chosen_set)):
      chosen_set.append(eg)
      g.add_edge(eg[0],eg[1])
      i+=1

g.show('Simple_Network_Graph.html')
display(HTML('Simple_Network_Graph.html'))

After the addition of edges, we will have a network graph that looks something like the one below. Look how amazing the network graph turns out to be and how interactive the graph is!


Conclusion

Pyvis is a powerful python module for visualizing and interactively manipulating network graphs using Python programming language. I hope you were able to build the network graphs using the library and enjoyed interacting with the graphs.

Thank you for reading!

Happy coding! 😃

Also Read: Network Analysis in Python – A Complete Guide


Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
Implementing Comic Visualization Python
Previous Post Implementing Comic Visualization Python
Next Post Building Network Graphs from Pandas' Dataframe
Building Network Graphs from Pandas’ Dataframe

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
cv2.imread: Load Images with OpenCV

cv2.imread: Load Images with OpenCV

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.