Home > Learning, Lesson, plotting, Problem, Projects/Experiments, Python > Python: Data Visualization Part-2

Python: Data Visualization Part-2

Learning : python, pygal, Data Visualization,Line Chart
Subject: Data visualization using pygal library

In this post we will talk about Line-chart using pygal library in python, Line-chart has three sub-type as: Basic, Stacked ,Time. We will use the data-set for Average age of Males and Females at first Marage during 6 yeaars (2000 and 2006), the code line to set the data data will be as :

line_chart.add(‘Males’, [30,20,23,31,39,44])

Line-chart: Basic
This is very normal and basic chart we use in all reports, we are feeding the data for Males and Females average age in first marage.. here is the code and the output ..


import pygal 

line_chart = pygal.Line()

line_chart.add('Males', [30,20,23,31,39,44]) 


line_chart.title = "Males and Females first Marage Age (average)"


Line-chart: Stacked Line Stacked chart (fill) will put all the data in top of each other. Here is the code.

Line-chart: Time Line Last type just to add or format the x_lables of the chart, we can use lambda function to do this (we can use lambda function with any other chart types), here we will do two example, one is using full time/date and another just write the month-year as string and will use the lambda function to calculate second data-set of Tax’s based on the salary amount..

import pygal 
from datetime import datetime, timedelta
d_chart = pygal.Line()

d_chart.add('Males', [30,20,23,31,39,44]) 

d_chart.x_labels = map(lambda d: d.strftime('%Y-%m-%d'), [
 datetime(2000, 1, 2),
 datetime(2001, 1, 12),
 datetime(2002, 3, 2),
 datetime(2003, 7, 25),
 datetime(2004, 1, 11),
 datetime(2005, 9, 5)])

d_chart.title = "Males and Females first Marage Age (average)"


To give better example of using lambda function, we will say: we have a salaries for 6 years (May-2000 to May-2006) and a Tax of 0.25, we will let the lambda function to calculate the Tax amount for each salary. Here is the code ..

 # Using lambda to calculate Tax amount

import pygal 
d_chart = pygal.Line()

d_chart.add('Salary', [550,980,1200,1800,2200,3500]) 

d_chart.add('Tax',map(lambda t: t*0.25, [550,980,1200,1800,2200,3500]))
d_chart.x_labels = map(str,(

d_chart.title = "Salary and Tax (0.25) payment in 6 years"


Next we will talk about Histogram chart.

:: Data Visualization using pygal ::

Line Chart
Part-3 Part-4

Follow me on Twitter..

By: Ali Radwani

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: