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

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


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.

  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. 


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.


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:




See Also
Altair Visualization


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


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.


Source Code for Family Tree


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. 

What Do You Think?

Join Our Telegram Group. Be part of an engaging online community. Join Here.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top