Hands-On Guide To Graphviz Python Tool To Define And Visualize Graphs

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.
Graphviz

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.

Subscribe to our Newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

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.

Implementation:

We will start by installing Graphviz using pip install graphviz.

  1. 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

  1. 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()

  1. 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’)

gra.node(‘b’, ‘RMSE’)

gra.node(‘c’, ‘MAE’)

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.

gra.edges([‘ab’, ‘ac’])

This will create the Edge between Graph objects, now let us visualize what we have created. 

gra

Basic Graph

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.

print(gra.source)

Source code in DOT language

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.

gra.render('Machine.gv.pdf', view=True)

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.

Downloaded Graph

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:

    i.attr(rank='same')

    i.node('A')

    i.node('X')

gra.node('C')

#Creating SubGraph with same level

with gra.subgraph() as i:

    i.attr(rank='same')

    i.node('B')

    i.node('D')

    i.node('Y')

#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.

gra

Family Tree

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.

print(gra.source)

Source Code for Family Tree

Conclusion:

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. 

Himanshu Sharma
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.

Download our Mobile App

MachineHack | AI Hackathons, Coding & Learning

Host Hackathons & Recruit Great Data Talent!

AIMResearch Pioneering advanced AI market research

With a decade of experience under our belt, we are transforming how businesses use AI & data-driven insights to succeed.

The Gold Standard for Recognizing Excellence in Data Science and Tech Workplaces

With Best Firm Certification, you can effortlessly delve into the minds of your employees, unveil invaluable perspectives, and gain distinguished acclaim for fostering an exceptional company culture.

AIM Leaders Council

World’s Biggest Community Exclusively For Senior Executives In Data Science And Analytics.

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
MOST POPULAR