📊 Matplotlib: Essential Graphs for Data
Science
1 Bar Graph
1️⃣
Definition:
A bar graph uses rectangular bars to show comparison between categories. Height/length of
each bar is proportional to its value.
Code Example:
import [Link] as plt
# Data
students = ["Aman", "Riya", "Karan", "Priya", "Neha"]
marks = [78, 85, 66, 90, 72]
# Plot
[Link](students, marks, color="blue", width=0.6)
# Customization
[Link]("Students Marks - Bar Graph")
[Link]("Students")
[Link]("Marks")
[Link]()
Attributes Used:
● color → bar color
● width → thickness of bars
Uses in Data Science:
● Compare categories (e.g., sales by product, marks by student)
● Visualize categorical data distribution
2️⃣Horizontal Bar Graph
Definition:
A horizontal bar graph displays data in horizontal bars, useful when category names are long.
Code Example:
import [Link] as plt
# Data
students = ["Aman", "Riya", "Karan", "Priya", "Neha"]
marks = [78, 85, 66, 90, 72]
# Plot
[Link](students, marks, color="green", height=0.5)
# Customization
[Link]("Students Marks - Horizontal Bar Graph")
[Link]("Marks")
[Link]("Students")
[Link]()
Attributes Used:
● color → bar color
● height → thickness of horizontal bars
Uses in Data Science:
● Compare categories with long labels
● Better visualization when many items exist
3️⃣Line Graph
Definition:
A line graph shows data points connected by straight lines, useful for trends over time.
Code Example:
import [Link] as plt
# Data
months = ["Jan", "Feb", "Mar", "Apr", "May"]
sales = [220, 340, 290, 410, 380]
# Plot
[Link](months, sales, color="red", linestyle="--", marker="o")
# Customization
[Link]("Monthly Sales - Line Graph")
[Link]("Month")
[Link]("Sales")
[Link](True)
[Link]()
Attributes Used:
● color → line color
● linestyle → --, -, : styles
● marker → point marker (o, *, s)
Uses in Data Science:
● Show trends and patterns over time
● Useful for time series analysis (stock prices, sales growth, weather data)
4️⃣Scatter Graph
Definition:
A scatter plot shows individual data points on the x–y plane to identify relationships or
correlations.
Code Example:
import [Link] as plt
# Data
hours = [2, 3, 4, 5, 6, 7, 8]
marks = [20, 35, 40, 50, 65, 70, 85]
# Plot
[Link](hours, marks, color="purple", s=100, marker="*")
# Customization
[Link]("Study Hours vs Marks - Scatter Plot")
[Link]("Hours Studied")
[Link]("Marks Scored")
[Link]()
Attributes Used:
● color → dot color
● s → size of points
● marker → shape of points
Uses in Data Science:
● Show correlation (positive, negative, or none)
● Identify outliers in data
● Used in regression analysis
5️⃣Pie Chart
Definition:
A pie chart shows proportions of categories as slices of a circle.
Code Example:
import [Link] as plt
# Data
companies = ["Google", "Amazon", "Microsoft", "Apple"]
share = [30, 25, 25, 20]
# Plot
[Link](share, labels=companies, autopct="%1.1f%%", startangle=90,
shadow=True)
# Customization
[Link]("Market Share - Pie Chart")
[Link]()
Attributes Used:
● labels → category names
● autopct → show percentage values
● startangle → rotate chart start
● shadow → add shadow effect
Uses in Data Science:
● Show percentage distribution
● Useful for market share, budget allocation, survey results
📌 Important Remaining Graphs in
Matplotlib (For Homework)
1 Histogram
1️⃣
● Definition: Shows the frequency distribution of continuous data using bins.
● Use in Data Science: Understanding distribution of features (e.g., age, marks, salaries).
● Homework: Generate 200 random numbers between 1–100 and plot a histogram with
10 bins.
2️⃣Box Plot (Whisker Plot)
● Definition: Displays median, quartiles, and outliers of a dataset.
● Use in Data Science: Detecting outliers and spread of data (useful in preprocessing).
● Homework: Create a box plot for exam scores of 20 students.
3️⃣Area Plot (Stack Plot)
● Definition: Line plot with the area below filled with colors.
● Use in Data Science: Showing cumulative or comparative trends (e.g., sales growth
over time).
● Homework: Plot the weekly study hours of 2 students using an area plot.
4️⃣Bubble Chart
● Definition: Scatter plot with varying point sizes to represent a third variable.
● Use in Data Science: Visualizing 3D relationships (e.g., income vs spending vs age).
● Homework: Plot students’ study hours (x), marks (y), and participation score (size).
📘 Practice Questions – Matplotlib (Only 5
Important Graphs)
1️⃣Line Graph
Plot the population growth of a town from 2010 to 2020:
Years = [2010,2012,2014,2016,2018,2020]
Population = [35, 40, 47, 55, 62, 70]
2️⃣Line Graph with Style
Plot the squares of numbers from 1 to 10 using a red dashed line with circle markers.
3️⃣Bar Graph
The sales of a shop in 5 months are given:
Months = ["Jan", "Feb", "Mar", "Apr", "May"]
Sales = [150, 200, 170, 220, 180]
Draw a bar chart.
4️⃣Bar Graph (Custom Color)
Draw a green bar chart showing marks of students:
Names = ["Aman","Riya","Karan","Priya","Neha"]
Marks = [78, 85, 66, 90, 72]
5️⃣Horizontal Bar Graph
Represent the above marks data using a horizontal bar chart.
6️⃣Scatter Plot
Hours studied vs marks scored:
Hours = [2,3,4,5,6,7,8]
Marks = [20,35,40,50,65,70,85]
Plot a scatter plot.
7️⃣Scatter Plot (Custom)
Plot the relationship between Age and Salary:
Age = [22,25,28,32,35,40,45]
Salary = [20,28,35,45,52,60,65]
8️⃣Pie Chart
Show the market share of companies using a pie chart:
Companies = ["Google","Amazon","Microsoft","Apple"]
Share = [30,25,25,20]
9️⃣Pie Chart (Custom Colors)
Plot a pie chart with % values for expenses:
Expenses = ["Food","Rent","Transport","Shopping"]
Values = [40,30,15,15]
🔟 Multiple Lines on Same Graph
Plot the sales of two products (A & B) over 5 months:
Months = ["Jan","Feb","Mar","Apr","May"]
Sales_A = [200,250,300,350,400]
Sales_B = [180,220,280,320,370]
Add legends for each line.