Pyvis to Create Interactive Network Graphs

Filed Under: Python Modules
Pyvis Python

Hey coder! In this tutorial, we will learn about a very interesting and fun way to visualize the social network graphs using the pyvis library in the Python programming library. Pyvis is a Python library that allows you to create interactive network graphs in a few lines of code.

Also read: Python NetworkX – Python Graph Library


Creating a Simple Network Graph Using Pyvis

We will start with a simple network of 5 nodes and 4 edges using the code below. We will code in Google collab and hence to visualize the network graph properly we convert the plot to html+javascript with the show method.

from pyvis import network as net
from IPython.core.display import display, HTML
g=net.Network(height='400px', width='100%',heading='A Simple Network Graph',bgcolor='black',font_color="white")
g.add_node(1)
g.add_node(2)
g.add_node(3)
g.add_node(4)
g.add_node(5)
g.add_edge(1,3)
g.add_edge(2,4)
g.add_edge(2,5)
g.add_edge(2,3)
g.show('example.html')
display(HTML('example.html'))

Look how amazing the interactivity is of the network graph we got from the pyvis library.


Creating Weighted Graphs Using Pyvis

We can also add weight to the edges and visualize how our plot looks when we take the weights into consideration using the code below. We will take the same network graph but add weights to the edges now using the value parameter of the add_edge function.

from pyvis import network as net
from IPython.core.display import display, HTML
g=net.Network(height='400px', width='100%',heading='A Weighted Network Graph',bgcolor='black',font_color="white")
g.add_node(1)
g.add_node(2)
g.add_node(3)
g.add_node(4)
g.add_node(5)
g.add_edge(1,3,value=5)
g.add_edge(2,4,value=2)
g.add_edge(2,5,value=8)
g.add_edge(2,3,value=4)
g.show('example.html')
display(HTML('example.html'))

You can observe how the weight of the edges is clearly visible in the network graph above.


Interactive Graph for a Networkx Dataset

Networkx comes with Zachary’s Karate Club Data which has 34 members of the club. Below we have a code that will help plot the data network graph using the pyvis library.

The dataset can be loaded into the program using the karate_club_graph function of the networkx library and the rest of the code remains almost the same.

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

G = nx.karate_club_graph()
g = net.Network(height='400px', width='100%',heading='Graph for Karate Club Dataset',bgcolor='black',font_color="white")
g.from_nx(G)
g.show('karate.html')
display(HTML('karate.html'))

Conclusion

Congratulations! In this tutorial, you have just learned how to create an amazing interactive network graph using pyvis. I hope this tutorial motivated you to start making your own network graphs!

And yes I won’t deny the fact that there are other ways to make graphs interactive, using much more advanced libraries but it’s the simplicity of pyvis that attracted me towards the module.

Thank you for reading! Hope you liked the tutorial!


close
Generic selectors
Exact matches only
Search in title
Search in content