0% found this document useful (0 votes)
32 views36 pages

Data Science with Python: NumPy, Pandas, Matplotlib

Uploaded by

carelegance9999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views36 pages

Data Science with Python: NumPy, Pandas, Matplotlib

Uploaded by

carelegance9999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Concepts of Data Science with

Python (NAI4501)
Module-3: Introduction To NumPy, Pandas and

Matplotlib

S
.R
Dr
2 5/
20

Dr. Radhey Shyam

Professor & Head


,

Department of Computer Science & Engineering (AI, CCML, and IOTBC)


10

Babu Banarasi Das University, Lucknow


st

Module-3 has been prepared and compiled by Dr. Radhey Shyam, with grateful acknowledgment to those who made their course contents

freely available or (Contributed directly or indirectly). Feel free to use this study material for your own academic purposes. For any query,
gu

communication can be made through this email : shyam0058@[Link].


Au

August 10, 2025


Program [Link] CSE (AI)
Year III Semester V
Course Name Concepts of Data Science with Python
Code NAI4501
Course Type PCC L T P Credit
Pre-Requisite Probability and statistics 2 1 0 3
1. Understand and use Python data structures – lists, tuples and
dictionaries.
Course 2. Understand the data science process and exploration.
Objectives 3. Evaluate Machine learning algorithms.
4. Analyse types of learning, processes, techniques and
models.
Course Outcomes
CO1 Understand the data science concepts, techniques and models.

S
Identify appropriate data visualization techniques given particular
CO2
requirements imposed by the data together with the driving questions.

.R
Build data graphics with the appropriate data visualization and analytics
CO3
software for the task at hand.
Student must be able to preprocess the data using cleaning, integration,
CO4

Dr
transformation and find correlations among the data.
5/
Contact Mapped
Module Course Contents
Hrs. CO
2
Introduction to Data Science Case for data science, data
science classification, data science algorithms; Data Science
20

Process, prior Knowledge, Data Preparation, Modeling, 30


1 CO1
Application, Knowledge; Data Exploration, Objectives of Hours
data Exploration; Datasets, Descriptive Statistics; Data
,
10

Visualization, Roadmap for data exploration.


Data Manipulation and Visualization
Introduction, Forms of data graphics, Visualizing categories
Design principles, Visualizing locations Interaction /
st

30 CO2,
2 animation, Visualizing locations, Visualizing time Building
Hours CO3
gu

and using metrics, Visualizing multivariate displays


Perception principles and presenting data graphics,
Visualizing multivariate displays.
Au

Introduction to NumPy, Pandas and Matplotlib, How to


Import NumPy module, what is a data Manipulation using
Panda’s library? Series object in pandas, Data Frame in 30
3 CO4
Pandas, loading a handling data with Pandas, Introduction to Hours
Matplotlib, Using Matplotlib for plotting Graphs and charts
like Scatter, Bar, Pie, Line, Histogram and more.

Suggested Readings

1. Data Science Fundamentals and Practical Approaches: Understand Why Data Science Is the Next
by Dr Gypsy Anand/ Dr Rupam Sharma.
2. Wes McKinney. “Python for Data Analysis”, O'Reilly Media, 2017, 2nd Edition.
3. Grus, Joel. “Data Science from Scratch: First Principles with Python”, O'Reilly Media, 2015, 1st
Edition.
4. Brian K. Jones and David M. Beazley. “Python Cookbook”, O'Reilly Media, 2013, 3rd Edition.
5. Dipanjan Sarkar, “Text Analytics with Python: A Practitioner‟s Guide to Natural Language
Processing”, A Press, 2019, 2nd Edition.
6. Daniel Jurafsky, James H. Martin, “Speech and Language Processing”, Pearson, 2009.

Online Resources

1. [Link]

Course Articulation Matrix


PO- PO
PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
PSO 1
CO1 1 1 2 2 1 2 1 1
CO2 2 1 3 1 2 1 1 1

S
CO3 1 1 2 1 3 2 2 2
CO4 1 2 2 2 1 1 1 1

.R
Dr
2 5/
, 20
10
st
gu
Au
1 Introduction

In the Python data science ecosystem, NumPy, Pandas, and Matplotlib form the core stack for:

ˆ Data storage and manipulation

ˆ Numerical and statistical computation

ˆ Data visualization and exploration

S
Goal: Understand the purpose, features, and usage of each library to prepare for real-world data

analysis tasks.

.R
2 NumPy – Numerical Python

What is NumPy?
Dr
5/
ˆ A library for numerical computations using arrays.
2
ˆ Provides fast, memory-efficient multidimensional arrays (ndarray).
20

ˆ Useful for mathematics, statistics, and matrix operations.


,
10

2.1 Key Features


Feature Description
st

Ndarray Efficient n-dimensional array object


gu

Broadcasting Arithmetic operations on arrays of different shapes


Vectorization Faster computation without Python loops
Au

Mathematical Tools Functions: mean, sum, std, sin, log etc.


Linear Algebra Matrix multiplication, inverse, transpose

2.2 Example

import numpy a s np

4
a r r = np . a r r a y ( [ [ 1 , 2 ] , [ 3 , 4 ] ] )

p r i n t ( ” Array : \ n ” , a r r )

p r i n t ( ” Mean : ” , np . mean ( a r r ) )

p r i n t ( ” Transpose : \ n ” , a r r . T)

3 Pandas – Python Data Analysis Library

What is Pandas?

S
ˆ Provides high-level data structures like Series and DataFrame

.R
ˆ Built on top of NumPy

Dr
ˆ Ideal for data manipulation, cleaning, and analysis 5/
3.1 Key Data Structures
Structure
2
Description
20

Series 1D labeled array


DataFrame 2D labeled table (rows × columns)
,
10

3.2 Common Operations


Pandas Function
st

Task

Read data read \ c s v ( ) , read \ e x c e l ( )


gu

View data head ( ) , t a i l ( ) , i n f o ( )


Au

Select columns d f [ ’ column ’ ] o r d f [ [ ’ c o l 1 ’ , ’ c o l 2 ’ ] ]

Filtering d f [ d f [ ’ Age ’ ] > 3 0 ]

Grouping groupby ( )

Aggregation mean ( ) , sum ( ) , count ( )

Handling missing data i s n u l l ( ) , f i l l n a ( ) , dropna ( )

5
3.3 Example

import pandas a s pd

d f = pd . r e a d c s v ( ’ data . csv ’ )

p r i n t ( d f . head ( ) )

p r i n t ( ” Average Age : ” , d f [ ’ Age ’ ] . mean ( ) )

S
4 Matplotlib – Visualization Library

.R
What is Matplotlib?

Dr
ˆ A plotting library for creating static, animated, and interactive visualizations in Python.

ˆ The main module is pyplot, often used as plt.


5/
4.1 Types of Plots
2
20

Plot Type Use Case

Line Plot Trends over time


,

Bar Chart Category comparison


10

Pie Chart Part-to-whole relationship

Histogram Distribution of values


st

Scatter Plot Relationship between two variables


gu

4.2 Basic Plot Example


Au

import m a t p l o t l i b . p y p l o t a s p l t

x = [1 , 2 , 3 , 4]

y = [ 1 0 , 15 , 13 , 17]

plt . plot (x , y)

6
p l t . t i t l e ( ” Simple Li ne P l o t ” )

p l t . x l a b e l ( ”X= a x i s ” )

p l t . y l a b e l ( ”Y= a x i s ” )

plt . grid ()

p l t . show ( )

4.3 Summary Comparison Table

S
Feature NumPy Pandas Matplotlib
Purpose Numerical computing Data manipulation Data visualization

.R
Main Structure Ndarray Series, DataFrame Figure, Axes
Strength Fast array math, broadcasting Intuitive data wrangling Visual storytelling with plots

Dr
Integration Foundation for Pandas & Scipy Built on NumPy Plots Pandas and NumPy outputs

4.4 Use in Data Science Workflow


5/
1. Import/Load Data → Pandas
2
20

2. Clean/Transform Data → Pandas + NumPy

3. Analyze Data Statistically → NumPy + Pandas


,
10

4. Visualize Trends & Insights → Matplotlib


st

5 How to Import NumPy Module in Python


gu

What is NumPy?
Au

NumPy (Numerical Python) is a fundamental library for numerical and scientific computing in

Python. It provides:

ˆ Fast and efficient array operations

ˆ Useful mathematical functions

ˆ Support for linear algebra, Fourier transforms, and random numbers

7
5.1 Installing NumPy (if not already installed)

Use pip (Python’s package installer) in your terminal or command prompt:

p i p i n s t a l l numpy

If you’re using Jupyter Notebook:

! p i p i n s t a l l numpy

S
5.2 Importing the NumPy Module

To use NumPy in your Python program, import it like this:

.R
import numpy a s np

Dr
np is the standard alias (short form) used by convention in the data science community. It allows

easier access to NumPy functions.


5/
5.3 Example Code
2
20

import numpy a s np
,

# C r e a t e a NumPy a r r a y
10

a r r = np . a r r a y ( [ 1 , 2 , 3 , 4 , 5 ] )

p r i n t ( ” Array : ” , a r r )
st
gu

# C a l c u l a t e mean

p r i n t ( ” Mean : ” , np . mean ( a r r ) )
Au

5.4 Verifying Installation

You can check the version of NumPy installed:

import numpy a s np

p r i n t ( np . version )

8
5.5 Summary
Code
Task

Install NumPy p i p i n s t a l l numpy

Import with alias import numpy a s np

Use function np . a r r a y ( ) , np . mean ( ) , e t c .

6 What is Data Manipulation Using Pandas Library?

S
Data manipulation is the process of loading, cleaning, transforming, and organizing data

.R
into a useful format for analysis and visualization. Pandas is the go-to Python library for data

manipulation due to its:

ˆ Intuitive syntax

Dr
5/
ˆ Powerful functions
2
ˆ Built-in support for real-world data formats (CSV, Excel, JSON, etc.)
20

6.1 Why Use Pandas for Data Manipulation?


,

Benefit
10

Feature
Works like Excel tables but more powerful
Easy handling of tabular data
For filtering, sorting, grouping, aggregating
High-level functions
st

Built-in tools for missing data f i l l n a ( ) , dropna ( ) , e t c .


gu

Integration with NumPy & Matplotlib


Seamless data analysis and plotting
Au

6.2 Core Data Structures


Structure
Description
Series 1D labeled array (like a column)
DataFrame 2D labeled table (like a spreadsheet)

9
6.3 Common Data Manipulation Tasks in Pandas
Pandas Function/Method
Task

Load data from CSV pd . r e a d c s v ( ’ f i l e . csv ’ )

View data d f . head ( ) , d f . t a i l ( ) , d f . i n f o ( )

Select columns/rows d f [ ’ column ’ ] , d f . i l o c [ 0 ] , d f . l o c [ 0 ]

Filter data d f [ d f [ ’ Age ’ ] > 3 0 ]

Add/modify columns d f [ ’ New ’ ] = d f [ ’ Old ’ ] * 2

S
Sort data d f . s o r t v a l u e s ( by=’ Score ’ , a s c e n d i n g=F a l s e )

Handle missing values d f . f i l l n a ( 0 ) , d f . dropna ( )

.R
Group and summarize d f . groupby ( ’ Category ’ ) . mean ( )

Merge and join tables pd . merge ( df1 , df2 , on=’ID ’ )

6.4 Example: Basic Data Manipulation Dr


5/
import pandas a s pd
2
20

# Load d a t a s e t

d f = pd . r e a d c s v ( ” s t u d e n t s . c s v ” )
,
10

# View t h e f i r s t few rows


st

p r i n t ( d f . head ( ) )
gu

# F i l t e r s t u d e n t s o l d e r than 18
Au

f i l t e r e d = d f [ d f [ ’ Age ’ ] > 1 8 ]

# Add a new column

d f [ ’ S c o r e P e r c e n t ’ ] = d f [ ’ Score ’ ] / d f [ ’ Max ’ ] * 100

# Group by c l a s s and f i n d a v e r a g e s c o r e

10
a v g s c o r e s = d f . groupby ( ’ C l a s s ’ ) [ ’ Score ’ ] . mean ( )

print ( avg scores )

6.5 Summary

Data manipulation using Pandas lets you:

ˆ Clean and prepare real-world messy datasets

S
ˆ Slice, filter, and reshape your data

.R
ˆ Generate insights through grouping, merging, and transformations

Dr
“If data is the new oil, Pandas is the refinery.” 5/
7 What is a Pandas Series?
2
A Series is a one-dimensional labeled array capable of holding any data type — integers, floats,
20

strings, Python objects, etc. Think of it as a column in a table or a 1D NumPy array with labels

(index).
,
10

7.1 Key Features of Series


Feature Description
st

1D array Holds data in a single dimension


gu

Labeled index Each data point is associated with a label (index)

Homogeneous All values usually share the same data type


Au

Fast operations Vectorized operations supported (like NumPy)

7.2 Creating a Series

import pandas a s pd

11
# From a l i s t

s 1 = pd . S e r i e s ( [ 1 0 , 2 0 , 3 0 , 4 0 ] )

p r i n t ( s1 )

Output:

0 10

1 20

2 30

S
3 40

dtype : i n t 6 4

.R
7.3 Custom Index in Series

Dr
s 2 = pd . S e r i e s ( [ 9 0 , 8 5 , 8 8 ] , i n d e x =[ ’ Math ’ , ’ S c i e n c e ’ , ’ E n g l i s h ’ ] )
5/
p r i n t ( s2 )
2
Math 90
20

Science 85

English 88
,
10

dtype : i n t 6 4
st

7.4 Accessing Elements


gu

p r i n t ( s 2 [ ’ Math ’ ] ) # A c c e s s by l a b e l

p r i n t ( s2 [ 1 ] ) # A c c e s s by p o s i t i o n
Au

7.5 Series from a Dictionary

marks = { ’ A l i c e ’ : 8 5 , ’ Bob ’ : 9 0 , ’ C h a r l i e ’ : 82}

s 3 = pd . S e r i e s ( marks )

p r i n t ( s3 )

12
7.6 Useful Series Methods
Method
Description
s . mean ( ) Returns mean of the values
s . max ( ) / s . min ( ) Max or min value
s . head ( n ) First n elements
s . t a i l (n) Last n elements
s . sort values () Sorts values

S
s . value counts () Frequency count of unique values
s . apply ( f u n c ) Applies a function element-wise

.R
7.7 Example with Operations

s = pd . S e r i e s ( [ 1 , 2 , 3 , 4 ] )
Dr
5/
# Add 5 t o each v a l u e
2
20

p r i n t ( s + 5)

# Square each v a l u e
,
10

p r i n t ( s . apply ( lambda x : x * * 2 ) )
st

7.8 Summary
gu

Aspect Description

Structure 1D labeled array


Au

Data Types int, float, string, object, etc.

Uses Represent columns, time series, etc.

Benefits Fast, intuitive, flexible

“Series is the building block of Pandas data structures — mastering it is the first

step toward data wrangling.”

13
8 What is a Pandas DataFrame?

A DataFrame is a two-dimensional, labeled tabular data structure in Pandas. It’s similar to:

ˆ A spreadsheet (like Excel),

ˆ A SQL table,

ˆ A dictionary of Series objects.

S
Each column is a Series, and all columns share the same row index.

.R
8.1 Key Features of DataFrame
Feature Description

Dr
2D structure Rows and columns, like a table
Labeled axes Custom row and column names
5/
Heterogeneous types Different data types per column (int, float, string, etc.)
Integrated methods Easy filtering, reshaping, joining, grouping, etc.
2
20

8.2 Creating a DataFrame


,

8.2.1 From a Dictionary


10

import pandas a s pd
st

data = {
gu

’Name ’ : [ ’ A l i c e ’ , ’ Bob ’ , ’ C h a r l i e ’ ] ,
Au

’ Age ’ : [ 2 5 , 3 0 , 2 8 ] ,

’ City ’ : [ ’ Delhi ’ , ’ Mumbai ’ , ’ Chennai ’ ]

d f = pd . DataFrame ( data )

print ( df )

14
8.2.2 From a List of Lists

data = [ [ ’ A l i c e ’ , 2 5 ] , [ ’ Bob ’ , 3 0 ] ]

d f = pd . DataFrame ( data , columns =[ ’Name ’ , ’ Age ’ ] )

8.2.3 From a CSV File

d f = pd . r e a d c s v ( ” data . c s v ” )

S
8.2.4 Accessing Data in DataFrame

.R
Code Example
Task

Select a column d f [ ’ Age ’ ]

Dr
Select multiple columns d f [ [ ’ Name ’ , ’ Age ’ ] ]

Select a row (by index) df . loc [ 0 ]


5/
Select by position df . i l o c [ 1 ]
2
Filter rows d f [ d f [ ’ Age ’ ] > 2 8 ]
20

8.2.5 Modifying DataFrame


,
10

# Add a new column

df [ ’ Salary ’ ] = [50000 , 60000 , 55000]


st

# Update a v a l u e
gu

d f . l o c [ 1 , ’ Age ’ ] = 31
Au

# Drop a column

d f . drop ( ’ City ’ , a x i s =1, i n p l a c e=True )

15
8.2.6 Useful DataFrame Methods

Method
Description
d f . head ( n ) First n rows
df . t a i l (n) Last n rows
d f . shape (Rows, Columns)
d f . columns List of column names
df . i n f o () Summary of the DataFrame

S
df . describe () Summary statistics (for numeric columns)
df . i s n u l l () Check for missing values

.R
d f . dropna ( ) Remove rows with nulls
df . f i l l n a ( value ) Fill nulls with a value

8.2.7 Iterating Over DataFrame


Dr
5/
f o r index , row i n d f . i t e r r o w s ( ) :
2
p r i n t ( f ”{ row [ ’ Name ’ ] } i s {row [ ’ Age ’ ] } y e a r s o l d . ” )
20

8.2.8 DataFrame vs Series


,
10

Feature Series DataFrame

Dimensionality 1D (single column) 2D (rows Ö columns)


st

Structure Labeled array Table with columns (each is Series)

Use Case Single column/variable data Full dataset/multiple variables


gu

8.2.9 Summary
Au

Aspect Value
Core structure 2D labeled data
Columns Represent different variables
Rows Represent observations/records
Strength Extremely flexible & powerful

“A Pandas DataFrame is your primary workspace in data analysis.”

16
9 Loading and Handling Data with Pandas

In data science, data often comes from external sources like CSV, Excel, SQL databases, JSON

files, or even online APIs. Pandas simplifies:

ˆ Loading data into a DataFrame

ˆ Performing cleaning, transformation, and exploration

Goal: Load, view, clean, and prepare data efficiently.

S
.R
9.1 Loading Data Using Pandas
Function Example
Source Type

Dr
CSV File pd . r e a d c s v ( ) d f = pd . r e a d c s v ( ” data . c s v ” )

Excel File pd . r e a d e x c e l ( ) d f = pd . r e a d e x c e l ( ” data . x l s x ” )


5/
JSON File pd . r e a d j s o n ( ) d f = pd . r e a d j s o n ( ” data . j s o n ” )

SQL Database pd . r e a d s q l ( ) d f = pd . r e a d s q l ( ”SELECT * FROM t a b l e ” , conn )


2
20

Clipboard pd . r e a d c l i p b o a r d ( ) d f = pd . r e a d c l i p b o a r d ( )

9.1.1 Viewing Data


,
10

Command
Purpose
d f . head ( n ) First n rows (default 5)
st

df . t a i l (n) Last n rows


gu

d f . shape Get dimensions (rows, columns)


d f . columns List of column names
Au

df . i n f o () Data types and null counts


df . describe () Summary statistics (mean, std, etc.)

9.1.2 Handling Missing Data

Real-world datasets often contain missing or null values.

17
[Link] Detecting Missing Data

df . i s n u l l () # Returns DataFrame o f True / F a l s e

d f . i s n u l l ( ) . sum ( ) # Count m i s s i n g v a l u e s p e r column

[Link] Filling or Dropping Nulls

d f . dropna ( ) # Drop rows with m i s s i n g v a l u e s

S
df . f i l l n a (0) # F i l l m i s s i n g v a l u e s with 0

d f . f i l l n a ( method=’ f f i l l ’ ) # Forward f i l l m i s s i n g v a l u e s

.R
9.1.3 Data Type Conversion

d f [ ’ Age ’ ] = d f [ ’ Age ’ ] . a s t y p e ( i n t )

Dr
5/
d f [ ’ Date ’ ] = pd . t o d a t e t i m e ( d f [ ’ Date ’ ] )
2
9.1.4 Filtering and Subsetting Data
20

# F i l t e r rows where Age > 25


,

d f f i l t e r e d = d f [ d f [ ’ Age ’ ] > 2 5 ]
10

# S e l e c t s p e c i f i c columns
st

d f s u b s e t = d f [ [ ’ Name ’ , ’ Age ’ ] ]
gu

9.1.5 Sorting Data


Au

d f . s o r t v a l u e s ( by=’ Score ’ , a s c e n d i n g=F a l s e )

9.1.6 Renaming Columns

d f . rename ( columns ={’OldName ’ : ’NewName’ } , i n p l a c e=True )

18
9.1.7 Saving Data to Disk

Command
Format

CSV d f . t o c s v ( ’ output . csv ’ , i n d e x=F a l s e )

Excel d f . t o e x c e l ( ’ output . x l s x ’ )

JSON d f . t o j s o n ( ’ output . j s o n ’ )

9.1.8 Example: Complete Workflow

S
import pandas a s pd

.R
# Load data

Dr
d f = pd . r e a d c s v ( ” s t u d e n t s . c s v ” )
5/
# Clean data

d f . dropna ( i n p l a c e=True )
2
d f [ ’ Score ’ ] = d f [ ’ Score ’ ] . a s t y p e ( i n t )
20

# F i l t e r and s o r t
,
10

h i g h s c o r e r s = d f [ d f [ ’ Score ’ ] > 8 0 ] . s o r t v a l u e s ( by=’ Score ’ , a s c e n d i n g=F a l s e )

# Save r e s u l t s
st

h i g h s c o r e r s . t o c s v ( ” t o p s t u d e n t s . c s v ” , i n d e x=F a l s e )
gu
Au

19
9.1.9 Summary

Function
Step

Load Data read csv () , read excel ()

Explore Structure head ( ) , i n f o ( ) , d e s c r i b e ( )

Clean Data dropna ( ) , f i l l n a ( ) , a s t y p e ( )

Subset and Filter d f [ column ] , d f [ c o n d i t i o n ]

Save Output to csv () , to excel ()

S
“Pandas makes data loading and cleaning seamless, turning messy data into pow-

.R
erful insights.”

Dr
10 Introduction to Matplotlib

Matplotlib is a data visualization library in Python that is used to create:


5/
ˆ Static, animated, and interactive plots
2
20

ˆ Charts for exploratory data analysis and presentation

It is the foundation for other libraries like Seaborn, Pandas plotting, and Plotly (to some extent).
,
10

The most used module is:

import m a t p l o t l i b . p y p l o t a s p l t
st
gu

10.1 Key Features of Matplotlib


Feature Description
Au

Line plots, bar charts, etc. Supports a wide range of 2D plots

Fine control Customize colors, labels, legends, ticks, etc.

Compatible with NumPy Works directly with arrays and Pandas objects

Interactive backends Works in Jupyter, Python scripts, GUIs, etc.

10.2 Basic Plot Structure

20
import m a t p l o t l i b . p y p l o t a s p l t

x = [1 , 2 , 3 , 4]

y = [ 1 0 , 20 , 25 , 30]

plt . plot (x , y)

p l t . t i t l e ( ” B a s i c L in e P l o t ” )

p l t . x l a b e l ( ”X= a x i s ” )

S
p l t . y l a b e l ( ”Y= a x i s ” )

.R
p l t . g r i d ( True )

p l t . show ( )

10.3 Common Plot Types in Matplotlib


Dr
5/
Function
Plot Type Use Case
2
Line Plot plt . plot () Trends over time
20

Bar Chart p l t . bar ( ) Comparing categories

Horizontal Bar p l t . barh ( ) Same as above with horizontal bars


,

Scatter Plot plt . scatter () Relationships between two variables


10

Pie Chart plt . pie () Part-to-whole relationships

Histogram plt . hist () Distribution of a variable


st

Box Plot pl t . boxplot () Distribution and outliers


gu

10.4 Customizing a Plot


Au

p l t . p l o t ( x , y , c o l o r =’ red ’ , l i n e s t y l e =’ == ’, marker =’o ’ )

p l t . t i t l e ( ” Customized P l o t ” )

p l t . x l a b e l ( ” Time ” )

p l t . y l a b e l ( ” Value ” )

p l t . l e g e n d ( [ ” Lin e A” ] )

21
p l t . g r i d ( True )

10.5 Subplots

Used to plot multiple plots in one figure:

p l t . subplot (1 , 2 , 1) # 1 row , 2 c o l s , 1 s t p l o t

plt . plot (x , y)

S
p l t . subplot (1 , 2 , 2) # 2nd p l o t

.R
p l t . bar ( x , y )

p l t . show ( )

10.6 Saving Plots to File


Dr
5/
p l t . s a v e f i g ( ” p l o t . png ” ) # Save a s PNG
2
p l t . s a v e f i g ( ” p l o t . pdf ” ) # Save a s PDF
20

10.7 Example: Multiple Plot Types


,
10

import m a t p l o t l i b . p y p l o t a s p l t
st

x = [1 , 2 , 3 , 4 , 5]
gu

y1 = [ 1 0 , 2 0 , 2 5 , 3 0 , 4 0 ]

y2 = [ 5 , 1 5 , 2 0 , 2 5 , 3 0 ]
Au

p l t . p l o t ( x , y1 , l a b e l =’ S a l e s ’ )

p l t . p l o t ( x , y2 , l a b e l =’ P r o f i t ’ )

p l t . t i t l e ( ” S a l e s vs P r o f i t ” )

p l t . x l a b e l ( ” Quarter ” )

p l t . y l a b e l ( ” Amount ” )

22
plt . legend ()

p l t . show ( )

10.8 Summary Table


Component
Purpose
plt . plot () Line plot
p l t . bar ( ) Bar chart

S
plt . scatter () Scatter plot
plt . t i t l e () Set title

.R
plt . xlabel () X-axis label
plt . ylabel () Y-axis label

Dr
plt . legend () Add legend
plt . grid () Show grid
5/
plt . savefig () Save figure to file
2
20

11 Using Matplotlib for Plotting Graphs


,

Matplotlib is a versatile and widely-used Python library for creating static, animated, and inter-
10

active plots. It is used for:

ˆ Data exploration
st

ˆ Presenting data visually


gu

ˆ Making insights more understandable


Au

The main plotting interface is:

import m a t p l o t l i b . p y p l o t a s p l t

11.1 Basic Plotting Structure

23
import m a t p l o t l i b . p y p l o t a s p l t

x = [1 , 2 , 3 , 4 , 5]

y = [ 1 0 , 12 , 8 , 14 , 7 ]

plt . plot (x , y)

p l t . t i t l e ( ” B a s i c L in e P l o t ” )

p l t . x l a b e l ( ”X= a x i s ” )

S
p l t . y l a b e l ( ”Y= a x i s ” )

.R
p l t . g r i d ( True )

p l t . show ( )

11.2 Line Graph Example


Dr
5/
x = [1 , 2 , 3 , 4]
2
y = [5 , 8 , 6 , 9]
20

p l t . p l o t ( x , y , c o l o r =’ blue ’ , marker =’o ’ , l i n e s t y l e =’ == ’)


,
10

p l t . t i t l e ( ” S a l e s Over Time ” )

p l t . x l a b e l ( ”Week” )

p l t . y l a b e l (” S a l e s ”)
st

p l t . g r i d ( True )
gu

p l t . show ( )
Au

11.3 Bar Chart Example

p r o d u c t s = [ ’ A’ , ’B’ , ’C ’ ]

s a l e s = [ 1 0 0 , 150 , 90]

p l t . bar ( p r o d u c t s , s a l e s , c o l o r =’ green ’ )

24
p l t . t i t l e ( ” Product S a l e s ” )

p l t . x l a b e l ( ” Product ” )

p l t . y l a b e l (” S a l e s ”)

p l t . show ( )

11.4 Pie Chart Example

l a b e l s = [ ’ Apples ’ , ’ Bananas ’ , ’ C h e r r i e s ’ ]

S
s i z e s = [ 3 0 , 45 , 25]

.R
p l t . p i e ( s i z e s , l a b e l s=l a b e l s , a u t o p c t = ’%1.1 f %%’, s t a r t a n g l e =90)

p l t . t i t l e (” Fruit S a l e s D i s t r i b u t i o n ”)

p l t . show ( )

Dr
5/
11.5 Scatter Plot Example
2
20

x = [ 1 0 , 20 , 30 , 40]

y = [ 5 , 15 , 10 , 20]
,
10

p l t . s c a t t e r ( x , y , c o l o r =’ red ’ )

p l t . t i t l e ( ” Study Hours vs Marks ” )


st

p l t . x l a b e l ( ” Study Hours ” )
gu

p l t . y l a b e l ( ” Marks ” )

p l t . show ( )
Au

11.6 Histogram Example

import numpy a s np

data = np . random . randn ( 1 0 0 0 )

25
p l t . h i s t ( data , b i n s =30 , c o l o r =’ p u r p l e ’ )

p l t . t i t l e ( ” D i s t r i b u t i o n o f Values ” )

p l t . x l a b e l ( ” Value ” )

p l t . y l a b e l ( ” Frequency ” )

p l t . show ( )

11.7 Box Plot Example

S
data = [ 7 , 8 , 5 , 6 , 1 0 , 9 , 6 , 4 , 9 , 1 1 ]

.R
p l t . b o x p l o t ( data )

p l t . t i t l e ( ” Boxplot o f S c o r e s ” )

p l t . show ( )

Dr
5/
11.8 Saving the Plot
2
20

p l t . s a v e f i g ( ’ my plot . png ’ ) # Saves t h e c u r r e n t p l o t t o a f i l e


,

11.9 Multiple Subplots


10

p l t . subplot (1 , 2 , 1)
st

plt . plot ([1 , 2 , 3] , [2 , 4 , 6])


gu

p l t . t i t l e ( ” L in e ” )
Au

p l t . subplot (1 , 2 , 2)

p l t . bar ( [ ’ A’ , ’B ’ ] , [ 3 , 5 ] )

p l t . t i t l e ( ” Bar ” )

plt . tight layout ()

p l t . show ( )

26
11.10 Summary Table
Command/Function
Task

Import module import m a t p l o t l i b . p y p l o t a s p l t

Line plot plt . plot ()

Bar chart p l t . bar ( )

Pie chart plt . pie ()

Scatter plot plt . scatter ()

S
Histogram plt . hist ()

Box plot pl t . boxplot ()

.R
Show plot p l t . show ( )

Save plot p l t . s a v e f i g ( ’ f i l e . png ’ )

12 Charts in Matplotlib: Scatter, Bar, Pie, Line, His- Dr


5/
togram, and More
2
20

Matplotlib supports a wide variety of charts and graphs useful for data visualization, pattern

recognition, and trend analysis.


,
10

Goal: Understand the syntax, purpose, and example use of each chart type.

12.1 Scatter Plot


st

To visualize relationships or correlations between two numerical variables.


gu

import m a t p l o t l i b . p y p l o t a s p l t
Au

x = [1 , 2 , 3 , 4 , 5]

y = [2 , 4 , 1 , 8 , 7]

p l t . s c a t t e r ( x , y , c o l o r =’ red ’ , marker =’o ’ )

p l t . t i t l e ( ” S c a t t e r P l o t Example ” )

27
p l t . x l a b e l ( ”X Values ” )

p l t . y l a b e l ( ”Y Values ” )

p l t . g r i d ( True )

p l t . show ( )

12.2 Bar Chart

To compare categories using rectangular bars.

S
c a t e g o r i e s = [ ’ A’ , ’B’ , ’C ’ ]

.R
values = [ 2 0 , 35 , 30]

p l t . bar ( c a t e g o r i e s , v a l u e s , c o l o r =’ blue ’ )

p l t . t i t l e ( ” Bar Chart Example ” )

p l t . x l a b e l ( ” Category ” ) Dr
5/
p l t . y l a b e l ( ” Values ” )
2
p l t . show ( )
20

12.3 Pie Chart


,
10

To show proportions or percentages of a whole.

l a b e l s = [ ’ Math ’ , ’ S c i e n c e ’ , ’ E n g l i s h ’ ]
st

s i z e s = [ 3 0 , 40 , 30]
gu

p l t . p i e ( s i z e s , l a b e l s=l a b e l s , a u t o p c t = ’%1.1 f %%’, s t a r t a n g l e =140)


Au

p l t . t i t l e ( ” Pi e Chart Example ” )

p l t . a x i s ( ’ eq ual ’ ) # Equal a s p e c t r a t i o f o r a c i r c l e

p l t . show ( )

12.4 Line Chart

To visualize trends over time or continuous variables.

28
x = [1 , 2 , 3 , 4 , 5]

y = [ 1 0 , 12 , 15 , 13 , 18]

p l t . p l o t ( x , y , c o l o r =’ green ’ , marker =’o ’ )

p l t . t i t l e ( ” L in e Chart Example ” )

p l t . x l a b e l ( ” Time ” )

p l t . y l a b e l (” S a l e s ”)

p l t . g r i d ( True )

S
p l t . show ( )

.R
12.5 Histogram

Dr
To display distribution of numerical data using bins.

import numpy a s np
2 5/
data = np . random . randn ( 1 0 0 0 )
20

p l t . h i s t ( data , b i n s =30 , c o l o r =’ p u r p l e ’ , e d g e c o l o r =’ black ’ )


,

p l t . t i t l e ( ” Histogram Example ” )
10

p l t . x l a b e l ( ” Value ” )

p l t . y l a b e l ( ” Frequency ” )
st

p l t . g r i d ( True )
gu

p l t . show ( )
Au

12.6 Box Plot

To summarize statistics and detect outliers.

data = [ 7 , 8 , 6 , 9 , 1 0 , 1 3 , 6 , 5 , 9 , 1 1 ]

p l t . b o x p l o t ( data )

29
p l t . t i t l e ( ” Box P l o t Example ” )

p l t . y l a b e l (” Scores ”)

p l t . show ( )

12.7 Area Plot (Stacked Plot)

To show parts of a whole over time.

x = [1 , 2 , 3 , 4 , 5]

S
y1 = [ 1 , 4 , 6 , 8 , 9 ]

.R
y2 = [ 2 , 2 , 7 , 1 0 , 1 2 ]

p l t . s t a c k p l o t ( x , y1 , y2 , l a b e l s =[ ’A’ , ’B ’ ] , c o l o r s =[ ’ s k y b l u e ’ , ’ orange ’ ] )

p l t . t i t l e ( ” Area P l o t Example ” )

p l t . l e g e n d ( l o c =’ upper l e f t ’ ) Dr
5/
p l t . show ( )
2
20

12.8 Horizontal Bar Chart

Same as bar chart, but horizontally for better readability of long labels.
,
10

l a n g s = [ ’ Python ’ , ’ Java ’ , ’C++’]

popularity = [ 4 5 , 30 , 20]
st

p l t . barh ( l a n g s , p o p u l a r i t y , c o l o r =’ t e a l ’ )
gu

p l t . t i t l e ( ” H o r i z o n t a l Bar Chart ” )

p l t . x l a b e l ( ” P o p u l a r i t y (%)”)
Au

p l t . show ( )

30
12.9 Comparison Table of Chart Types
Function
Chart Type Use Case

Line Chart Trends and time series plt . plot ()

Bar Chart Category comparison p l t . bar ( )

Horizontal Same as bar, but for wide labels p l t . barh ( )

Pie Chart Show parts of a whole plt . pie ()

Scatter Plot Correlation between two variables plt . scatter ()

S
Histogram Frequency distribution plt . hist ()

pl t . boxplot ()

.R
Box Plot Summary statistics + outliers

Area Plot Part-of-whole trends plt . stackplot ()

Dr
2 5/
, 20
10
st
gu
Au

31
Au
gu
st
10
, 20
2 5/
Dr
.R
S
Au
gu
st
10
, 20
2 5/
Dr
.R
S
Au
gu
st
10
, 20
2 5/
Dr
.R
S
Au
gu
st
10
, 20
2 5/
Dr
.R
S
References

[1] URL [Link] [NPTEL] Access

23.07.2025

[2] URL [Link] [NPTEL] Access 23.07.2025

[3] URL [Link] [NPTEL]Access 23.07.2025

[4] URL [Link] Access 23.07.2025

S
[5] URL [Link] Access 23.07.2025

.R
[6] URL [Link] Access 28.03.2025

Dr
********************
2 5/
, 20
10
st
gu
Au

36

You might also like