Archive
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(‘Females’,[22,25,18,35,33,18])
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('Females',[22,25,18,35,33,18]) line_chart.add('Males', [30,20,23,31,39,44]) line_chart.x_labels=map(str,range(2000,2006)) line_chart.title = "Males and Females first Marage Age (average)" line_chart.render()
![]() |
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('Females',[22,25,18,35,33,18]) 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)" d_chart.render()
![]() |
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,( 'May-2001','May-2002', 'May-2003','May-2004', 'May-2005','May-2006')) d_chart.title = "Salary and Tax (0.25) payment in 6 years" d_chart.render()
![]() |
Next we will talk about Histogram chart.
:: Data Visualization using pygal ::
Part-1Bar-Chart | Part-2 Line Chart | Part-3 | Part-4 |
By: Ali Radwani
Python ploting
Learning : Plotting Data using python and numpy
Subject: Plotting Data
The best way to show the data is to make them as a graph or charts, there are several charts type and names each will present your data in a different way and used for different purpose. Plotting the data using python is a good way to show out your data and in coming posts we will cover very basic aspects in plotting data. So if we just want to show a sample for what we are talking about, we will say: we have a sample of hospital data for born childs (male m, female f, in years 200 to 2003).
:: Click to enlarge :: ![]() |
There are some libraries we can use in python to help us plotting the data, here are some of them. Matplotlib, Plotly and Seaborn are just samples of what we may use, in this post we will use the Matplotlib. To use Matplotlib we need to install it, so if it is not installed in your python you need to do so.
pip install Matplotlib
Then we need to import it in our code using :
import matplotlib.pyplot as plt
To show the data we need to have some variables that will be used in our first example, So the case is that we have some data from a hospital, the data are numbers of born childs (male m, female f) in years 2000 to 2003. We will store/save the data in list, we will have data_yesrs =[2000,2001,2002,2003], then we will have male born data in data_m=[2,2.5,3,5] and female born data data_f = [3,3.8,4,4.5], the chart will have two axis vertical is Y y_data_title =’In Hundreds’ and horizontal is X x_data_title =’ Years’, now to project all this information on a chart we use this code ..
import matplotlib.pyplot as plt data_yesrs = [2000,2001,2002,2003] # years on X axis data_m = [2,2.5,3,5] # y data males born data_f = [3,3.8,4,4.5] # y data female born y_data_title ='In Thousands' x_data_title =' Years' plt.title('New Born babies') plt.plot(data_yesrs,data_m,'r-', data_yesrs,data_f,'b--') plt.ylabel(y_data_title) plt.xlabel(x_data_title) plt.show()
Another way to plot the data were we can use a one line for each data set as:
plt.plot(data_x,data_m,’r-‘)
plt.plot(data_x,data_f,’b–‘)
We can see that male data is red line, and female data is blue dashes, we can use some line style to present the data as mentioned bellow:
‘-‘ or ‘solid’ is solid line
‘–‘ or ‘dashed’ is dashed line
‘-.’ or ‘dashdot’ is dash-dotted line
‘:’ or ‘dotted’ is dotted line
‘None’ or ‘ ‘ or ” is draw nothing
And also we can use colors such as :
r: red, g: green,
b: blue, y: yellow .
If we want to add the map or chart key, we need first to import matplotlib.patches as mpatches then to add this line of code:
plt.legend([‘Male’,’Female’])
and the keys [‘Male’,’Female’] MUST be in the same sequence as the main plot code line :
plt.plot(data_yesrs,data_m,’r-‘, data_yesrs,data_f,’b–‘)
![]() |
To Download my Python code (.py) files Click-Here