Introduction to Pyvis and Creating a Basic Network Graph
Interactive network graphs are a powerful tool for visualizing complex data sets and relationships. The Pyvis library, a Python wrapper for the Javascript library vis.js, offers a user-friendly way to generate interactive network graphs with Python code.
In this article, we’ll explore how to create a basic network graph using Pyvis and then enhance it with node properties.
Creating a Basic Network Graph with Pyvis
To create a network graph with Pyvis, we start by importing the Network class from the pyvis.network module. We then create a new Network object and add nodes and edges to it.
The add_node and add_edge methods take node and edge objects as arguments, respectively. Nodes are represented as dictionaries that define their attributes, such as their label, color, and shape.
Edges are represented as tuples that define the source and target nodes, along with any edge-specific attributes such as label or color. Here’s an example of how to create a basic network graph with Pyvis:
from pyvis.network import Network
net = Network()
# add nodes
net.add_node(1, label="Node 1")
net.add_node(2, label="Node 2")
net.add_node(3, label="Node 3")
# add edges
net.add_edge(1, 2, label="Edge 1-2")
net.add_edge(2, 3, label="Edge 2-3")
net.show("basic_network_graph.html")
This will create a basic network graph with three nodes and two edges, and save it as an HTML file.
When we open the file in a browser, we can interact with the graph by clicking and dragging nodes, zooming in and out, and hovering over nodes and edges to see their labels.
Enhancing the Network Graph with Node Properties
While the basic network graph is functional, it lacks visual interest and clarity. In this section, we’ll explore how to enhance the graph with node properties such as labels, sizes, colors, shapes, and border widths.
Node Labels
The label attribute is a string that defines the node’s text label. By default, nodes are labeled with their node number.
To set a custom label, we can pass the label attribute to the add_node function.
net.add_node(1, label="Node A")
net.add_node(2, label="Node B")
net.add_node(3, label="Node C")
This will set the node labels to “Node A”, “Node B”, and “Node C”, respectively.
Scaling Node Sizes for Better Visualization
To better represent the relative importance or size of nodes, we can scale their size based on a value attribute. The value attribute is a numeric value that scales the node size, with larger values resulting in larger nodes.
net.add_node(1, label="Node 1", value=10)
net.add_node(2, label="Node 2", value=20)
net.add_node(3, label="Node 3", value=30)
This will scale the node sizes relative to their values, resulting in Node 1 being the smallest and Node 3 being the largest.
Adding a Splash of Color to Your Nodes
Color is another attribute that can make a network graph more visually appealing and informative. The color attribute is a string that defines the node’s fill color, specified in hexadecimal format.
net.add_node(1, label="Node 1", color="#ff0000")
net.add_node(2, label="Node 2", color="#00ff00")
net.add_node(3, label="Node 3", color="#0000ff")
This will set the node colors to red, green, and blue, respectively.
Diversifying Node Shapes for Visual Interest
In addition to the default “dot” shape, Pyvis provides several other node shape options, including ellipse, circle, database, box, and text. The shape attribute is a string that defines the node’s shape.
net.add_node(1, label="Node 1", shape="ellipse")
net.add_node(2, label="Node 2", shape="database")
net.add_node(3, label="Node 3", shape="text")
This will set Node 1 to an ellipse shape, Node 2 to a database shape, and Node 3 to a text shape.
Defining Node Border Width for Clarity
Finally, we can define the border width and color of nodes to improve clarity and contrast against the background and edges. The borderWidth attribute is a numeric value that defines the border width, in pixels.
net.add_node(1, label="Node 1", borderWidth=2)
net.add_node(2, label="Node 2", borderWidth=4)
net.add_node(3, label="Node 3", borderWidth=6)
This will set Node 1’s border width to 2 pixels, Node 2’s to 4 pixels, and Node 3’s to 6 pixels.
Conclusion
Interactive network graphs can be a powerful way to explore and understand complex data. The Pyvis library provides a user-friendly and flexible way to create interactive network graphs with Python code.
By enhancing our network graphs with node properties such as labels, sizes, colors, shapes, and borders, we can improve their visual appeal and clarity, and gain deeper insights into our data. 3) Bringing it All Together: The Complete Code
So far, we’ve explored how to create a basic network graph with Pyvis and how to enhance it with node properties.
In this section, we’ll put it all together and create a complete network graph with all the attributes we’ve discussed.
Creating a Complete Network Graph with Pyvis
To create a complete network graph, we’ll start with the basic code from our first section and add all the additional attributes we’ve covered.
from pyvis.network import Network
net = Network()
# add nodes
net.add_node(1, label="Node 1", color="#ff0000", shape="circle", value=10, borderWidth=2)
net.add_node(2, label="Node 2", color="#00ff00", shape="database", value=20, borderWidth=4)
net.add_node(3, label="Node 3", color="#0000ff", shape="box", value=30, borderWidth=6)
# add edges
net.add_edge(1, 2, label="Edge 1-2", color="#000000")
net.add_edge(2, 3, label="Edge 2-3", color="#000000", dashes=True)
# physics buttons
net.show_buttons(filter_=['physics'])
# directed network graph
net.barnes_hut()
net.set_options(directed=True)
# Complete attributes
net.set_options(height="800px", width="100%", min_height="400px", min_width="600px", margin={"left": "20px", "right": "20px", "top": "20px", "bottom": "20px"})
from IPython.display import display
from IPython.display import HTML
display(HTML('
Complete Network Graph with Pyvis
'))
display(net.show("complete_network_graph.html"))
First, we define node 1 with a label “Node 1”, red color “#ff0000”, circular shape, size of 10 with a border width of 2 pixels.
Similarly, node 2 is defined with a label “Node 2”, green color “#00ff00”, database shape, size of 20 with a border width of 4 pixels. Finally, node 3 is defined with a label “Node 3”, blue color “#0000ff”, box shape, size of 30 with a border width of 6 pixels.
Next, we add edges from node 1 to node 2 with a label “Edge 1-2” and a black color “#000000”. The second edge is added from node 2 to node 3 with a label “Edge 2-3”, a black color “#000000”, and a dashes attribute set to True, which creates a dashed line for the edge.
We then add physics buttons to our graph by calling the show_buttons() method with the filter_ parameter set to “physics”. This adds buttons to the graph interface that allow us to turn on or off physics-based interactions like gravity, repulsion, and attraction between nodes.
We then define the graph layout using Barnes-Hut force-directed algorithm, which uses a hierarchical quadtree algorithm to speed up the layout of large graphs. We set the options to directed=True, which tells Pyvis to create a directed network graph as opposed to an undirected one.
Finally, we set some additional options to control the graph’s height, width, minimum height and width, and margin. We then display the network graph in an IPython notebook using the show() method and then display the HTML code that generates the graph and labels it as a complete network graph with Pyvis.
4) Conclusion
In conclusion, Pyvis is a powerful library that allows us to create interactive and visually appealing network graphs with just a few lines of code. By experimenting with node properties such as labels, sizes, colors, shapes, and borders, we can make our graphs more informative and intuitive to read.
This tutorial covered the basics of creating a network graph with Pyvis, enhancing it with node properties, and creating a complete graph with all the attributes we’ve discussed. With Pyvis, we can create complex network graphs in a simple and intuitive way, making it an invaluable tool for anyone working with complex data.
In this article, we explored the power of Pyvis library in creating interactive and visually appealing network graphs with just a few lines of code. We started with creating a basic network graph and then enhanced it with node properties such as labels, sizes, colors, shapes, and borders.
We then brought all these attributes together to create a complete network graph with Pyvis. Network graphs are powerful tools for visualizing complex data sets and relationships.
Pyvis makes it easy to create and customize these graphs, allowing us to explore and understand our data in a visually intuitive way. The takeaway is that Pyvis is a valuable tool for anyone working with complex data, and understanding how to use it can greatly enhance our data analysis and presentation skills.