Pyvis: Visualize Interactive Network Graphs in Python | by Khuyen Tran | Towards Data Science

Imagine you survey your employees about how much they like other employees on a scale from 1 to 10. However, you find it hard to understand the relationships between your employees by looking at the table below.
Wouldn’t it be nice if you could visualize their connections using an interactive network graph like below?
That is when pyvis comes in handy.
Pyvis is a Python library that allows you to create interactive network graphs in a few lines of code.
To install pyvis, type:
pip install pyvis
To add nodes to the network graph, simply use net.add_node(id, label) . id is unique to each node. label is used to display the node’s label in the graph.
After running net.show('nodes.html') , a file named nodes.html should be saved to your current directory. You can interact with it like below.
To view the graph in your Jupyter Notebook, simply add the parameternotebook=True toNetwork .
If you have many nodes, you might wish to add a list of nodes instead. That could be done with the add_nodes method.
Nice! Let’s make our nodes look a little bit more interesting by adding colors to them.
Having nodes alone is not fun. Let’s figure out how to connect these nodes together with edges.
To add edges to the graph, use theadd_edge method. This method takes the source of the edge and the edge destination.
To add multiple edges at once, useadd_edges , whose parameter is a list of tuples.
Cool! But what if some edges have larger weights than the others (.i.e, Oliver might like Alex more than Michael)?
We can specify the weight of each edge by adding the parameter value to add_edge or by adding the third value to each tuple of add_edges.
Now edges connecting to a specific node will be highlighted when you select that node.
Node distance is the distance between nodes. Spring length is the rest length of the edges.
We can adjust the distance between nodes and the spring length by using the repulsion method. Let’s see how the plot changes as we change the parameter node_distance and the parameter spring_length .
Default:
Increase spring length:
Increase node distance:
Let’s apply what we learn so far to solve the problem mentioned at the beginning of the article.
First, let’s get the nodes.
['Ben', 'Kate', 'Thinh', 'Jorge', 'Alfredo', 'Francisco', 'Olivia', 'Chris']
Next, we write a function to get a list of edges with weights.
We also write a function to create the entire network graph.
Now we’re ready to visualize our data!
There are a lot of edges in the network graph above. Let’s make the graph more readable by only showing the edges with weights above a certain value.
Cool! Now we have a better understanding of the connections of these employees.
We can also change the network setting using the configuration UI. This allows us to quickly experiment with different layouts and graph physics.
Setting filter_=True will show 3 widgets (nodes, edges, physics). If you want to show only the physics widget, use filter_=['physics'] :
Congratulations! You have just learned how to create a network graph using pyvis. I hope this article will give you the motivation to create your own network graph. Since it only takes several lines of code to create an interactive network graph, why not give it a try?
Feel free to fork and play with the code for this article in this repo:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: