Summary
Pyvis is a Python library that enables users to create interactive network graphs with just a few lines of code.
Abstract
Pyvis is a Python library that simplifies the creation of interactive network graphs, making it easier for users to visualize data relationships. The library can be installed using pip, and nodes can be added to the graph using unique IDs and labels. Multiple nodes can be added at once using lists, and edges can be added to connect nodes, with the option to specify edge weights. The node distance and spring length can be adjusted using the repulsion method, and Pyvis can be used to visualize real-world social networks, such as Facebook data.
Opinions
Given a table showing the friendships between people in a group, you are assigned to split that group into groups of two. However, it is difficult to do so by looking at the table.
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 pyvisTo 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 have learned to visualize a real social network. We will use Facebook data, which consists of friend lists from Facebook.
Facebook data was collected from survey participants, and the users in this data have been anonymized.
You can download the data from here. After downloading the data, unzip and save it as facebook_combined.txt .
data = pd.read_csv("facebook_combined.txt", sep=" ", header=None)
data.columns = ["person1", "person2"]Since this is a large dataset, we will only visualize 1000 samples from the data.
sample = data.sample(1000, random_state=1)
sample.head(10)
Visualize the network:
net = Network(
notebook=True,
cdn_resources="remote",
bgcolor="#222222",
font_color="white",
height="750px",
width="100%",
)
nodes = list(set([*sample.person1, *sample.person2]))
edges = sample.values.tolist()
net.add_nodes(nodes)
net.add_edges(edges)
You can highlight a node and its adjacent edges and nodes by setting select_menu=True and filter_menu=True :
net = Network(
notebook=True,
cdn_resources="remote",
bgcolor="#222222",
font_color="white",
height="750px",
width="100%",
select_menu=True,
filter_menu=True,
)
net.add_nodes(nodes)
net.add_edges(edges)
We can also change the network setting using the configuration UI. This allows us to experiment quickly with different layouts and graph physics.

Setting filter_=True will show three 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 motivate you to create your network graph. Since it only takes several lines of code to create an interactive network graph, why not try?
Feel free to fork and play with the code for this article in this repo:
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 the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:
Riccardo AndreoniLearn how companies like Facebook and LinkedIn extract insights from networks
John Loewen, PhDPrompting GPT-4 for multi-visual interactive dashboard creation
Ritesh GuptaStories, strategies, and secrets to choosing the perfect algorithm.
Tari IbabaWow I never thought the day I stop using VS Code would come so soon…