MITB Banner

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

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.

Implementation:

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

df.head()

  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.render_to_file('bar1.svg')

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

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

bar_graphs.render_to_file('bar2.svg')

  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.render_to_file('line1.svg')

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.render_to_file('line2.svg')

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.render_to_file('box1.svg')

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.render_to_file('dot1.svg')

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.render_to_file('funnel1.svg')

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.render_to_file('gauge.svg')

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.render_to_file('treemap1.svg')

Treemap Visualization

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

Conclusions:

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.

Picture of Himanshu Sharma

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

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

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
Recent Stories

Featured

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

AIM Conference Calendar

Immerse yourself in AI and business conferences tailored to your role, designed to elevate your performance and empower you to accomplish your organization’s vital objectives. Revel in intimate events that encapsulate the heart and soul of the AI Industry.

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed