Active Hackathon

Complete Tutorial on Pygal: A Python Tool for Interactive and Scalable visualization

Pygal creates a variety of plots like a bar, area, histogram, line, etc and due to the high scalability of the images of the plot downloaded as SVG
Pygal Visualization Tutorial

Data visualization is a scientific method of finding out the patterns, anomalies, and outliers in the data. It is a method that is used to create the graphs and plots which helps in finding out the trend in the data because it makes data simpler in terms of understanding by the human brain and we identify trends with naked eyes. 

Python provides different visualization packages that help in creating different types of charts, graphs, and plots. Pygal is an open-source python library that not only creates highly interactive plots but also creates SVG images of the graphs/plots so that we can use it and customize it accordingly. Pygal is highly customizable and creates graphs with a few lines of code. 


Sign up for your weekly dose of what's up in emerging technology.

Pygal creates a variety of plots like a bar, area, histogram, line, etc and due to the high scalability of the images of the plot downloaded as SVG, we will have good quality images that can be embedded into different projects, websites, etc.

In this article, we will go through different charts that we can create using pygal and also see how we can download them in the SVG format.


We will start exploring pygal but before that, we need to install pygal using pip install pygal.

  1. Importing required libraries

We will be importing pygal for visualization and pandas for importing the dataset for which we will be creating the visualizations.

import pandas as pd

import pygal

  1. Loading the Dataset

We will be working with an Advertisement dataset that contains different attributes like ‘TV’, ‘Radio’, etc. and a target variable ‘Sales’.

df = pd.read_csv(‘Advertising.csv’)


  1. Creating Bars and Graphs

Now we will use pygal to create different bars and graphs using the dataset we have loaded. We will start by creating some basic charts and then move to some advance charts and downloading all the graphs and charts in SVG formats.

  1. Bar Charts

Bar charts represent the data in the form of horizontal or vertical bars with the values they represent. Pygal supports different types of Bar charts.

bar_graphs = pygal.Bar()   

bar_graphs.add('Sales', df['Sales'][:5]) 


bar_graphs.add('Sales', df['Sales'][:5])

bar_graphs.add('TV', df['TV'][:5])


  1. Line charts

A line chart displays the data or information as a series of data points on a line. 

line_chart = pygal.Line()

line_chart.add('Sales', df['Sales'][:15])


line_chart.add('Sales', df['Sales'][:15])

line_chart.add('TV', df['TV'][:15])

line_chart.add('Newspaper', df['Newspaper'][:15])

line_chart.add('Radio', df['Radio'][:15])


Line Chart Visualization
  1. Box Plots

Box Plots are considered as the statistical graphs which are used to display the numerical data with their quartile information. They are generally used to visualize the outliers in the data.

box_plot = pygal.Box()

box_plot.title = 'Advertising'

box_plot.add('TV', df['TV'])

box_plot.add('Radio', df['Radio'])

box_plot.add('Newspaper', df['Newspaper'])


Box Plot Visualization
  1. Dot Plots

A dot plot is a statistical plot that is used to display the data in forms of the dot, the size of the dot represents the values i.e higher the value bigger is the dot. 

dot_chart = pygal.Dot()

dot_chart.title = 'Advertising'

dot_chart.add('Sales', df['Sales'][:5])

dot_chart.add('TV', df['TV'][:5])

dot_chart.add('Radio', df['Radio'][:5])

dot_chart.add('Newspaper', df['Newspaper'][:5])


Dot Chart Visualization
  1. Funnel Charts

Funnel charts are used to visualize the progressive reduction or expansion of data when it passes from different phases. It is generally used to represent the Sales data.

funnel_chart = pygal.Funnel()

funnel_chart.title = 'Advrtising'

funnel_chart.add('TV', df['TV'][:15])

funnel_chart.add('Radio', df['Radio'][:15])

funnel_chart.add('Newspaper', df['Newspaper'][:15])


Funnel Chart Visualization
  1. Gauge Chart

A gauge chart combines the doughnut chart and pie chart, it is used to visualize the data at a particular value. 

gauge_chart = pygal.Gauge(human_readable=True)

gauge_chart.title = 'Advertising'

gauge_chart.range = [0, 500]

gauge_chart.add('Sales', df['Sales'][:1])

gauge_chart.add('TV', df['TV'][:1])

gauge_chart.add('Radio', df['Radio'][:1])

gauge_chart.add('Newspaper', df['Newspaper'][:1])


Gauge Chart Visualization
  1. Treemap

Treemaps are used to represent data in nested forms usually in rectangles shape. The size of the map resent the value i.e higher the value greater is the size of the treemap.

treemap = pygal.Treemap()

treemap.title = 'Advertisement TreeMap'

treemap.add('Sales', df['Sales'][:10])

treemap.add('TV', df['TV'][:10])

treemap.add('Radio', df['Radio'][:10])

treemap.add('Newspaper', df['Newspaper'][:10])


Treemap Visualization

Pygal provides different types of parameters for different graphs, we can manipulate different designs, features, and render them accordingly.


In this article, we learned about Pygal, a visualization package in python which provides highly interactive and scalable visualizations. We saw how we can create different charts and plots from basic to advanced in just a few lines of code. We downloaded the images in SVG format which makes it scalable for using it in different applications or websites.

More Great AIM Stories

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.

Our Upcoming Events

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

3 Ways to Join our Community

Discord Server

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

Telegram Channel

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

Subscribe to our newsletter

Get the latest updates from AIM