BipinRupadiya.
com 1
GUJARAT TECHNOLOGICAL UNIVERSITY
MASTER OF COMPUTER APPLICATIONS (MCA)
SEMESTER: III
Subject Name: Programming in Python
Subject Code : 4639304
BipinRupadiya.com 2
Unit-4
Advanced Topics I:
Plotting and Data Science
BipinRupadiya.com 3
Python
Chapter-11
Plotting
Plotting using PyLab, Plotting mortgages
and extended examples
Text Book:
John V Guttag. “Introduction to Computation and Programming Using Python”, Prentice Hall of India
BipinRupadiya.com
Plotting using PyLab
PyLab is a module that belongs to the Python mathematics
library Matplotlib.
PyLab combines the numerical module numpy with the
graphical plotting module pyplot.
PyLab was designed with the interactive Python interpreter in
mind, and therefore many of its functions are short and
require minimal typing.
This makes it a very efficient and convenient mathematical
tool.
If you want to install PyLab, you must also install Matplotlib.
BipinRupadiya.com 5
Installation procedure of libraries
Before we start the chapter, first install library like pandas
,matplotlib, xlrd, numpy to your system.
For this below command one by one you need to write in ‘cmd’
Cmd path must be like this only.
BipinRupadiya.com
Installation procedure of libraries
C:\> pip install numpy
C:\> pip install pandas
C:\> pip install xlrd
C:\> pip install matplotlib
Update library if needed, updation code will come in cmd from
there you can copy the code.
C:\> python -m pip install --upgrade pip
BipinRupadiya.com
Example-1
BipinRupadiya.com
Example-2
BipinRupadiya.com
Example-3
BipinRupadiya.com 10
Change the default values:“rc settings.”
It is also possible to change
the default values, which
are known as “rc settings.”
(The name “rc” is derived
from the .rc file extension
used for runtime
configuration files in Unix.)
These values are stored in a
dictionary-like variable that
can be accessed via the
name pylab.rcParams.
BipinRupadiya.com 11
rc settings
Example
BipinRupadiya.com 12
Output
BipinRupadiya.com 13
Plotting mortgages
and
extended examples
BipinRupadiya.com 14
Mortgage:-
Noun:-
a legal agreement by which a bank, building society, etc. lends money at
interest in exchange for taking title of the debtor's property, with the condition
that the conveyance of title becomes void upon the payment of the debt.
"I put down a hundred thousand in cash and took out a mortgage for the rest“
Verb:-
convey (a property) to a creditor as security on a loan.
"the estate was mortgaged up to the hilt"
BipinRupadiya.com
Python
Chapter-25
Data Science Using Python
Data Frame (Creating Data Frame from an Excel Spreadsheet, Creating Data Frame from .csv
Files, Creating Data Frame from a Python Dictionary, Creating Data from Python List of
Tuples, Operations on Data Frames),
Data Visualization : Bar Graph, Histogram, Creating a Pie Chart, Creating Line Graph
Text Book:
R Nageswara Rao, Core Python Programming, 2nd Edition, Dreamtech Press
BipinRupadiya.com
Introduction
Data plays an important role in our lives.
For example, a chain of hospitals contain data related to
medical report and prescription of their patients.
Every piece of data is precious , as it may affect the
business organization which is using this data.
So we need some mechanism to store the data.
For example company have data of sales department,
production department, purchase department, ..etc.
Such a data is stored in ‘warehouse’. We can assume
warehouse as a central repository of integrated data from
different sources.
BipinRupadiya.com
Data analysis
Once the data is stored , we must be able to retrieve the
data based on some pre-requisite.
There will be huge amount of data, so we retrieve the
data as per the need of organization.
This is called data analysis or data analytics where
the data that is retrieved will be analyzed to answer the
questions raised by the management of the organization.
A person who does analysis is known as ‘data analyst’.
BipinRupadiya.com
Data analysis and data visualization
BipinRupadiya.com
Data visualization
Once the data is analyzed , duty of IT professional is to
represent the data in the form of graphs.
So the management will be able to understand it easily.
Such a graphs will be helpful them to forecast the future
of company.
This is called data visualization.
The primary goal of data visualization is to communicate
information clearly and efficiently using statistical
graphs, plots and diagrams.
BipinRupadiya.com
Data science:
Data science is used for techniques to extract information
from the data warehouse, analyze them and present
necessary data to the business organization in order to
arrive at important conclusion and decision.
A person who is involved in this work is called ‘data
scientist’.
BipinRupadiya.com
Data Scientist vs. Data analyst
Data Scientist Data Analyst
Data Scientist formulates the questions Data analyst receives questions from
that will help a business organization the business team and provides
and then proceed in solving them answers to them
Data scientist will have strong data Data analyst simply analyzes the data
visualization skill and the ability to and provides information requested by
convert data into a business story. team.
Perfection in mathematics, statistics Perfection in data warehousing, big
and programming languages like data concepts, SQL and business
Python and R are needed for a data intelligence is needed for a data
scientist. analyst.
Data scientist estimate the unknown Data analyst looks at the known data
information from the known data. from a new perspective.
BipinRupadiya.com 22
Data Frame:
Data Frame is an object that is useful in representing data
in the form of rows and column.
For example ,
Data may come from a file or an excel sheet or from python
sequence like tuple and list.
Once the data is stored in data frame , we can perform
many operation on it that are useful in analyzing and
understanding the data.
Python contain pandas, xlrd packages those are useful for
data analysis and manipulation process.
BipinRupadiya.com
Data Frame object
index Column names
Empid Name Salary DOJ
0 1001 Hitesh 100000 01-01-2008
1 1002 Mitesh 200000 01-01-2009
2 1003 Ritesh 300000 01-01-2010
3 1004 Jitesh 400000 01-01-2011
4 1005 Sitesh 500000 01-01-2012
Data
(different type in each column)
BipinRupadiya.com 24
Creating
Data Frame from an Excel Spreadsheet
BipinRupadiya.com
Creating
Data Frame from an Excel Spreadsheet
BipinRupadiya.com
Creating Data Frame from a .csv file
BipinRupadiya.com
Creating
Data Frame from a python Dictionary
BipinRupadiya.com
Creating
Data Frame from a python List of Tuples
BipinRupadiya.com
Operations
on
Data Frames
BipinRupadiya.com
shape
BipinRupadiya.com
head()
BipinRupadiya.com
tail()
BipinRupadiya.com 33
Retrieving range of rows
DataFrame[start : end : step]
BipinRupadiya.com 34
Retrieving range of rows from
reverse order
BipinRupadiya.com 35
Retrieving column names
BipinRupadiya.com 36
Retrieving data from column
BipinRupadiya.com 37
Retrieving data from columns
BipinRupadiya.com 38
Find min() and max() value of column
BipinRupadiya.com 39
describe()
Display statistical information of data frame
BipinRupadiya.com 40
Working with index of data frame
BipinRupadiya.com 41
Setting
a column as index of data frame
BipinRupadiya.com 42
Data Frame .loc[] attribute
BipinRupadiya.com 43
Reset index in data frame
BipinRupadiya.com 44
Sorting Data
BipinRupadiya.com 45
Multi Column Sorting
BipinRupadiya.com 46
Handling Missing Data
BipinRupadiya.com
Drop Missing Data
BipinRupadiya.com
Handling Missing Data
BipinRupadiya.com
Data Visualization
When data is shown in the form of pictures or graphs, it
becomes easy for the user to understand it.
So this way representation of data is known as ‘data
visualization’.
For this we need, pyplot submodule of the matplotlib
module.
BipinRupadiya.com
Four type of graphs
Bar Graph
Histogram
Pie Chart
Line Graph
BipinRupadiya.com
Bar Graph
It represents data in the form of vertical or horizontal bar.
It is useful to compare the quantity.
We need to create the data frame for this.
BipinRupadiya.com
Bar Chart Example
BipinRupadiya.com 53
Output
BipinRupadiya.com 54
Horizontal Bar Chart
BipinRupadiya.com 55
Output
BipinRupadiya.com 56
Bar Graph
We can create the bar graphs for from more than
one data set that are coming from multiple data
frames.
BipinRupadiya.com
Example : Bar Graph
from multiple data frames
BipinRupadiya.com
Output
BipinRupadiya.com 59
Histogram
It shows distribution of values.
It is similar to bar graph but it is useful to show values grouped in
bins or intervals.
For example
we can collect age of employee and show it into histogram
to know how many employees are there in the range of
0-10 years,
10-20 years,
20-30 years…
etc
BipinRupadiya.com
Histogram
BipinRupadiya.com
Example
BipinRupadiya.com 62
Pie Chart
It shows a circle that is divided into sectors and each sector
represents a proportion of the whole.
For example
we can take different departments in company and their employees.
Suppose there are 4 departments and their employees are in the
percentage of 50%,20%,15% and 15%.
These can be represented as slice in the pie chart.
BipinRupadiya.com
Pie Chart
plt.pie(slice,labels=depts,colors=cols,startangle=90,explode=(0,0.2,0,0),
shadow=True, autopct='%.1f%%')
labels=a list of labels
Colors=list of colors
startangle=90
indicates pie chart will start at 90 degrees(12 o’clock position).
Default it start with 0 degree(3’o clock position).
shadow=True
indicates pie chart should be displayed with a shadow. This will improve the look of a
chart.
autopct='%.1f%%‘
indicates how to display percentages on slice.
Here , %.1 indicates display one digit after decimal point.
The next two % symbol indicates only one %symbol should be displayed rather than
two.
BipinRupadiya.com
Pie Chart
BipinRupadiya.com
Output
Pie Chart
BipinRupadiya.com 66
Line Graph
It shows the result in the form of lines.
To create a line graph , we need x and y coordinates.
#create a pie chart
plt.plot(years, profits, 'blue')
Here years=x axis
Profits=y axis
blue=color used for line in graph
BipinRupadiya.com
Line Graph
BipinRupadiya.com
Output
BipinRupadiya.com 69
Bipin S. Rupadiya
(MCA, PGDCA, BCA)
Assistant Professor,
JVIMS-MCA (537), Jamnagar
www.BipinRupadiya.com
BipinRupadiya.com 70