Adventures in Machine Learning

Visualize Complex Relationships with Pyvis Library Network Graphs

Pyvis Library: Anto Network Graphs with Interactivity

Network graphs, also known as graph theory or network theory, are a collection of nodes and edges that are interconnected in specific ways. Pyvis Library is a powerful Python tool that can create, modify and display network graphs with ease.

This library allows users to create interactive and dynamic network graphs that can be shared as HTML files or embedded in web pages. In this article, we will explore the Pyvis Library, how to install it, and how to create an empty network graph and add nodes.

Description of Pyvis Library

Pyvis Library is a Python package that allows users to create beautiful and interactive network graphs. It is built on top of the popular D3.js library, which is a JavaScript library for manipulating documents based on data.

Pyvis Library provides an easy-to-use interface for creating and modifying network graphs, with a variety of options for customizing the graphs properties.

Installation of Pyvis Library

To start using Pyvis Library, you need to install it in your Python environment. The easiest way is to use pip, the Python package installer.

Open your command prompt and type the following command:

“`python

pip install pyvis

“`

This will install Pyvis Library and its dependencies.

Creation of Empty Network Graph

An empty network graph can be created using the `Network()` function, which is provided by Pyvis Library. The arguments of the `Network()` function are optional and can be used to define the properties of the graph.

“`python

from pyvis.network import Network

g = Network(height=’500px’, width=’700px’)

g.show(‘network.html’)

“`

The above code creates an empty network graph with a height of 500 pixels, a width of 700 pixels, and saves it to a file named `network.html`. When you run this code, you will see an empty network graph on your screen.

Adding Nodes Using `add_node()` Function

To add nodes to the network graph, you need to use the `add_node()` function provided by Pyvis Library. The first argument of the function is the name of the node, and the second argument is an optional dictionary that can be used to define the properties of the node.

The properties can include the color, shape, size, label, and HTML format of the node. “`python

from pyvis.network import Network

g = Network(height=’500px’, width=’700px’)

g.add_node(‘Node 1’)

g.add_node(‘Node 2’)

g.add_node(‘Node 3’)

g.show(‘network.html’)

“`

The above code adds three nodes to the network graph with the default properties.

When you run this code, you will see three nodes on your screen. You can modify the properties of the nodes by adding a dictionary as the second argument of the `add_node()` function.

The properties can be customized according to your needs. “`python

from pyvis.network import Network

g = Network(height=’500px’, width=’700px’)

g.add_node(‘Node 1′, color=’red’, size=20, shape=’circularImage’, image=’https://randomuser.me/api/portraits/men/1.jpg’, label=’John Doe’)

g.add_node(‘Node 2′, color=’green’, size=30, shape=’circularImage’, image=’https://randomuser.me/api/portraits/women/1.jpg’, label=’Jane Smith’)

g.add_node(‘Node 3′, color=’blue’, size=40, shape=’circularImage’, image=’https://randomuser.me/api/portraits/men/2.jpg’, label=’Mike Johnson’)

g.show(‘network.html’)

“`

The above code adds three nodes to the network graph with customized properties.

You can see that each node has a different color, size, shape, image, and label. When you run this code, you will see three nodes with their respective properties on your screen.

You can also add nodes in HTML format, which allows you to add more complex visualizations to the network graph. “`python

from pyvis.network import Network

g = Network(height=’500px’, width=’700px’)

html = “””

Node 1

This is the description of Node 1

“””

g.add_node(“Node 1”, label=html)

g.show(‘network.html’)

“`

The above code adds a node to the network graph in HTML format.

You can see that the node has a custom design and layout. When you run this code, you will see one node with its respective HTML format on your screen.

Conclusion

Pyvis Library is a powerful tool for creating, modifying, and displaying network graphs with Python. You can install it easily using pip, and create an empty network graph with the `Network()` function.

Adding nodes to the network graph is straightforward using the `add_node()` function, and you can customize the properties of the nodes according to your needs. You can also add nodes in HTML format, allowing you to create more complex visualizations.

With Pyvis Library, you can create interactive and dynamic network graphs that can be shared with others.

Adding Edges to Network Graph

In network graphs, the edges represent the relationships or connections between the nodes. In Pyvis Library, you can add edges to the network graph using the `add_edge()` function.

Additionally, the `generate_edge()` function can be used to randomly generate pairs of source and destination nodes. In this section, we will explain how to add edges to the network graph using these functions.

Generating Random Source and Destination Nodes Pair

To generate a random pair of source and destination nodes, you can use the `generate_edge()` function provided by Pyvis Library. The `generate_edge()` function takes two arguments: the number of edges you want to generate and the list of node ids from which you want to select the source and destination nodes pair.

“`python

from pyvis.network import Network

import random

g = Network(height=’500px’, width=’700px’)

nodes = [“Node 1”, “Node 2”, “Node 3”, “Node 4”]

edges = []

for i in range(5):

edge = g.generate_edge(nodes)

edges.append(edge)

print(edges)

“`

The above code creates a network graph with four nodes and generates five pairs of source and destination nodes using the `generate_edge()` function. The nodes are stored in the `nodes` list, and the generated edges are stored in the `edges` list.

When you run this code, you will see five pairs of source and destination nodes on your screen.

Adding Edges to Network Graph

To add edges to the network graph, you need to use the `add_edge()` function provided by Pyvis Library. The `add_edge()` function takes four arguments: the source node id, the destination node id, the edge weight (optional), and a dictionary that can be used to customize the properties of the edge.

“`python

from pyvis.network import Network

g = Network(height=’500px’, width=’700px’)

nodes = [“Node 1”, “Node 2”, “Node 3”, “Node 4”]

edges = [(1, 2, 1), (1, 3, 2), (2, 3, 3), (2, 4, 4), (3, 4, 5)]

for node in nodes:

g.add_node(node, color=’green’, size=30)

for edge in edges:

src, dest, weight = edge

g.add_edge(src, dest, value=weight)

g.show(‘network.html’)

“`

The above code adds five edges to the network graph with the properties customized using a dictionary. The nodes are created with green color and a size of 30.

The five edges are specified by five tuples, where each tuple contains the source node, destination node, and edge weight. The `add_edge()` function is called in a loop to add each edge to the network graph.

The edge weight is used to specify the width of the edge. When you run this code, you will see the five edges on your screen with customized properties.

You can modify the properties of the edges by adding a dictionary as the fourth argument of the `add_edge()` function. The properties can be customized according to your needs, such as changing the color of the edges or adding tooltips.

“`python

from pyvis.network import Network

g = Network(height=’500px’, width=’700px’)

nodes = [“Node 1”, “Node 2”, “Node 3”, “Node 4”]

edges = [(1, 2), (1, 3), (2, 3), (2, 4), (3, 4)]

for node in nodes:

g.add_node(node, color=’green’, size=30)

for edge in edges:

src, dest = edge

g.add_edge(src, dest, color=’#000000′, width=1, title=’Edge’)

g.show(‘network.html’)

“`

In the above code, the `add_edge()` function is used to add five edges to the network graph with customized properties. The edges are specified by five tuples, where each tuple contains the source node and destination node.

The properties of the edges are defined in the custom dictionary in the `add_edge()` function. Here, we set the color of the edges to black, the width of the edges to 1, and add a tooltip with the title ‘Edge’.

When you run this code, you will see the five edges on your screen with their respective properties.

Conclusion

In summary, this article provided an overview of Pyvis Library, which is a powerful tool for creating, modifying, and displaying network graphs using Python programming language. We explained the process of generating random source and destination nodes pairs using the `generate_edge()` function, and adding edges to the network graph using the `add_edge()` function.

In addition, we also showed how to customize the properties of the edges using a dictionary. With Pyvis Library, you can create interactive and dynamic network graphs with ease and can be shared with others.

In this article, we explored Pyvis Library, a powerful tool for creating, modifying, and displaying network graphs using Python programming language. We discussed installing the library and creating an empty network graph with the `Network()` function.

We covered adding nodes with the `add_node()` function and customizing their properties. We also explained adding edges to the network graph with the `add_edge()` function and generating random source and destination nodes pair with the `generate_edge()` function.

With Pyvis Library, it is easy to create interactive and dynamic network graphs that can be shared with others and customized according to our needs. The key takeaway is that Pyvis Library is a versatile and efficient tool that simplifies the process of network graph visualization.