2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
Matplotlib by Rishabh Mishra
In [ ]: # What is matplotlib?
# Matplotlib is a Python library used for creating static, animated, and interac
# Data visualization: It's the graphical representation of data to identify patt
0 Install Matplotlib
In [ ]: !pip install matplotlib
In [4]: import [Link] as plt
# pyplot is a collection of functions that make matplotlib work as MATLAB
1 Create First Chart
In [5]: # data
x = [1,2,3,4,5]
y = [10, 20, 25, 35, 45]
[Link](x,y) # line chart
[Link]() # display chart in jp nb
2 Customize Chart
In [18]: [Link](figsize=(4,3)) # figure size
[Link](x, y, color='blue', marker='o', linestyle='--', linewidth=2, markersize
[Link] by Rishabh Mishra (3).html 1/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
[Link]("Kuch bhi Title hai")
[Link]("x-axis label hai")
[Link]("y-axis label hai")
[Link]()
3 Advanced - Multiple Lines & Legends
In [20]: # data
x = [1,2,3,4,5]
y1 = [10, 20, 25, 35, 45]
y2 = [20, 30, 35, 45, 55]
[Link](x, y1, label='Sales 2024') # ploting y1 data
[Link](x, y2, label='Sales 2025') # ploting y2 data
[Link]("YoY Sales")
[Link]("Months")
[Link]("Sales")
[Link]() # show legends
[Link]()
[Link] by Rishabh Mishra (3).html 2/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
4 Bar Chart
In [22]: # data
x = ['A','B','C','D','E']
y = [10, 20, 55, 35, 45]
[Link](x,y)
[Link]('Bar Chart Example')
[Link]()
[Link] by Rishabh Mishra (3).html 3/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
5 Histogram
In [27]: # used for distribution analysis
# data
import random
data = [[Link](1, 20) for _ in range(500)]
[Link](data, bins=20) # histogram chart
[Link]("Histogram Example")
[Link]()
[Link] by Rishabh Mishra (3).html 4/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
6 Pie chart
In [32]: # used to show part-to-whole relationships/data
# data
categories = ['A','B','C','D','E']
sales = [10, 20, 55, 35, 45]
[Link](sales, labels=categories, autopct = '%1.1f%%', startangle=90)
[Link]("Pie Chart Example")
[Link]()
[Link] by Rishabh Mishra (3).html 5/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
7 Scatter plot
In [33]: # used to find relationship btwn variables
# data
y1 = [10, 20, 25, 35, 45]
y2 = [20, 30, 35, 45, 55]
[Link](y1, y2)
[Link]("Scatter Plot Example")
[Link]()
[Link] by Rishabh Mishra (3).html 6/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
8 Subplots
In [44]: # used to show multiple charts in one figure
# data-1 - bar chart
categories = ['Mon','Tue','Wed','Thu','Fri']
sales = [10, 20, 55, 35, 45]
# data-2 - scatter plot
y1 = [10, 20, 25, 35, 45]
y2 = [20, 30, 35, 45, 55]
[Link](figsize=(10,4))
# first plot- bar chart
[Link](1,2,1) # row, column, position
[Link](categories, sales)
[Link]("Daily Sales")
[Link]("Week Days")
[Link]("Sales")
# second plot- scatter chart
[Link](1,2,2) # row, column, position
[Link](y1, y2)
[Link]("User Example")
[Link]("User1")
[Link]("User2")
[Link]()
[Link] by Rishabh Mishra (3).html 7/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
9 Matplotlib with Pandas - real data
In [47]: # create df
import pandas as pd
# data
data = {
'Month' : ['Jan', 'Feb', 'Mar', 'Apr'],
'Sales' : [12000, 11000, 13000, 25000]
}
df = [Link](data)
df
Out[47]: Month Sales
0 Jan 12000
1 Feb 11000
2 Mar 13000
3 Apr 25000
In [48]: [Link](df['Month'], df['Sales'])
[Link]("Matplotlib with Pandas")
[Link]("Month")
[Link]("Sales")
[Link]()
[Link] by Rishabh Mishra (3).html 8/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
10 Save charts
In [49]: [Link](df['Month'], df['Sales'])
[Link]("Matplotlib with Pandas")
[Link]("Month")
[Link]("Sales")
[Link]("Monthly_Sales_from_df.png")
[Link]()
[Link] by Rishabh Mishra (3).html 9/10
2/2/26, 11:07 PM Matplotlib by Rishabh Mishra
HW: Use object-oriented interface in Matplotlib
Notes: [Link]
Python Libraries YT Playlist: [Link]
list=PLdOKnrf8EcP169c-IjEbxgGqnpeXgkJKp
In [ ]:
[Link] by Rishabh Mishra (3).html 10/10