Python provides different visualization libraries that allow us to create different graphs and plots. These graphs and plots help us in visualizing the data patterns, anomalies in the data, or if data has missing values. Visualization is an important part of data discovery.
Modules like seaborn, matplotlib, bokeh, etc. are all used to create visualizations that are highly interactive, scalable, and visually attractive. But these libraries don’t allow us to create nodes and edges to connect different diagrams or flowcharts or a graph. For creating graphs and connecting them using nodes and edges we can use Graphviz.
Graphviz is an open-source python module that is used to create graph objects which can be completed using different nodes and edges. It is based on the DOT language of the Graphviz software and in python it allows us to download the source code of the graph in DOT language.
In this article, we will see how we can create a graph using Graphviz and how to download the source code of the graph in the DOT language.
We will start by installing Graphviz using pip install graphviz.
- Importing Required Libraries
The digraph is defined under graphviz which we will use for creating graphs object, nodes, and edges. We will create different sample graphs. We will import digraph.
from graphviz import Digraph
- Initializing the digraph
After importing the digraph the next step is to initialize digraph by creating a graph object. Let us create a graph object.
gra = Digraph()
- Create Graphs
For creating graphs we will use the dot and edges function and create different types of graphs. Initially, we will start by creating a node for the graph.
gra.node(‘a’, ‘Machine Learning Errors’)
This will create different graph nodes, now we need to connect these nodes with edges and then visualize them. Let’s create the edges for these graph objects.
This will create the Edge between Graph objects, now let us visualize what we have created.
Here You can see how we created the graph objects(nodes) and then connect them using the edges. Now let us see how we can see the source code of the graph we created.
This is the source code that can be used in DOT language to render the graph using graphviz graph drawing software. We can also save and render the source code using render function. Let us see how we can save it.
This will create a pdf with the graph which we created and the name which we have assigned.
If we open the pdf which we have created in the above step we will have the output given below.
Now let us see one more example and create a new graph. Let us create a family tree and see how we can visualize it.
gra = Digraph(filename='Family_Tree.gv') #Filename
#Creating the Subgtraph with same rank
with gra.subgraph() as i:
#Creating SubGraph with same level
with gra.subgraph() as i:
#Connecting the edges of the graph
gra.edges(['XC', 'AC', 'CD', 'XY', 'XD', 'XB'])
This is how we have created the family tree now let us visualize it.
Here you can see the graph objects we created linked to each other using edges. Now let us see the source code for this graph.
In this article, we saw how graphviz is used to create graphs/flowcharts using nodes and edges. We saw how to visualize these graphs, render these graphs to a file and also how to download the source code in DOT language. Graphviz can be used to create many more complex graphs that can be used for different purposes as per requirements.
Join Our Telegram Group. Be part of an engaging online community. Join Here.
Subscribe to our NewsletterGet the latest updates and relevant offers by sharing your email.
An aspiring Data Scientist currently Pursuing MBA in Applied Data Science, with an Interest in the financial markets. I have experience in Data Analytics, Data Visualization, Machine Learning, Creating Dashboards and Writing articles related to Data Science.