Imagine your school has examination results for 500 students. The school wants to know which subject students performed best in and which subject needs improvement.
Reading hundreds of scores one after another may take a long time. But what if you could turn the information into colourful charts? You would understand the results much faster.
This is called Data Visualisation. It is the process of presenting information using charts, graphs, and other visual methods.
In this module, you will learn how to create charts using Python, Pandas, and Matplotlib. You will also complete a practical data analysis project.
By the end of this module, you should be able to:
Data visualisation means presenting information in a visual format so that people can understand it easily.
Instead of reading a long list of numbers, we can use a chart to compare values, identify patterns, and understand changes.
Example:
A shop owner records the number of products sold every day. A chart can help the owner quickly identify the busiest sales day.
Data visualisation helps us to:
A bar chart uses rectangular bars to compare different categories. For example, it can compare the number of students in different classes.
When to use it: When comparing different groups or categories.
A line chart uses points connected by lines to show how information changes over time.
For example, a business can use a line chart to show its monthly sales.
When to use it: When showing trends over time.
A pie chart is a circular chart divided into sections. Each section represents a portion of the whole.
For example, a pie chart can show how a family spends its monthly income.
When to use it: When showing how categories contribute to a whole.
A histogram groups numerical values into ranges and shows how often values fall within each range.
For example, a teacher can use a histogram to understand how students' examination scores are distributed.
When to use it: When studying the distribution of numerical data.
Matplotlib is a Python library used to create charts and graphs. It helps data analysts turn numbers into visual information.
Before using Matplotlib, you may need to install it. Open your terminal or command prompt and type:
pip install matplotlib
Import the library into your Python program:
import matplotlib.pyplot as plt
The name plt is a short name commonly used when working
with Matplotlib.
Let's imagine a school wants to compare the number of students enrolled in four different subjects.
import matplotlib.pyplot as plt
subjects = ["Mathematics", "English", "Science", "ICT"]
students = [40, 50, 35, 45]
plt.bar(subjects, students)
plt.title("Students Enrolled in Each Subject")
plt.xlabel("Subjects")
plt.ylabel("Number of Students")
plt.show()
What does this program do?
plt.bar() to create a bar chart.plt.show() to display the chart.A line chart helps us understand how something changes over time. Let's create a chart showing a shop's sales for six months.
import matplotlib.pyplot as plt
months = ["January", "February", "March",
"April", "May", "June"]
sales = [50000, 65000, 55000, 80000, 90000, 110000]
plt.plot(months, sales, marker="o")
plt.title("Monthly Shop Sales")
plt.xlabel("Months")
plt.ylabel("Sales in Naira")
plt.show()
The line connects the monthly sales figures. This helps the shop owner see when sales increased or decreased.
A pie chart helps us understand how different categories make up a complete total.
Imagine a student spends money on transport, food, data, and savings. We can use a pie chart to show how the money is shared.
import matplotlib.pyplot as plt
expenses = ["Transport", "Food", "Data", "Savings"]
amounts = [5000, 10000, 3000, 7000]
plt.pie(amounts, labels=expenses, autopct="%1.1f%%")
plt.title("Monthly Student Expenses")
plt.show()
The autopct setting displays the percentage represented
by each section of the pie chart.
A histogram helps us understand how numerical values are spread across different ranges.
Let's visualise students' examination scores:
import matplotlib.pyplot as plt
scores = [45, 50, 55, 60, 65, 70, 72, 75,
80, 82, 85, 90, 92, 95]
plt.hist(scores, bins=5)
plt.title("Distribution of Examination Scores")
plt.xlabel("Scores")
plt.ylabel("Number of Students")
plt.show()
The histogram groups scores into ranges. It helps the teacher understand where most students' scores are concentrated.
A good chart should be easy to understand. We can improve our charts by adding titles, labels, legends, and grid lines.
import matplotlib.pyplot as plt
months = ["January", "February", "March", "April"]
sales = [20000, 30000, 25000, 45000]
plt.plot(months, sales, marker="o", label="Sales")
plt.title("Monthly Sales Report")
plt.xlabel("Month")
plt.ylabel("Sales in Naira")
plt.legend()
plt.grid(True)
plt.show()
Here is what the additional commands do:
plt.title() adds a title.plt.xlabel() labels the horizontal axis.plt.ylabel() labels the vertical axis.plt.legend() displays the chart legend.plt.grid() adds grid lines.In earlier modules, you learned how to organise information using Pandas. We can combine Pandas and Matplotlib to create charts directly from a DataFrame.
import pandas as pd
import matplotlib.pyplot as plt
data = {
"Product": ["Rice", "Bread", "Milk", "Sugar"],
"Sales": [50000, 30000, 25000, 40000]
}
shop = pd.DataFrame(data)
shop.plot(
x="Product",
y="Sales",
kind="bar",
legend=False
)
plt.title("Sales by Product")
plt.xlabel("Products")
plt.ylabel("Sales in Naira")
plt.show()
This example creates a DataFrame and uses Pandas' plotting feature to create a bar chart.
Creating a chart is only one part of data analysis. You must also understand what the chart is showing.
When studying a chart, ask yourself:
Congratulations! You have reached the final practical project in this course.
Imagine you are a junior data analyst working for a small supermarket in Ibadan. The shop owner wants to understand which products generate the most sales.
import pandas as pd
import matplotlib.pyplot as plt
data = {
"Product": ["Rice", "Bread", "Milk", "Sugar", "Beans"],
"Price": [5000, 1500, 2500, 3000, 4000],
"Quantity": [20, 40, 25, 15, 18]
}
shop = pd.DataFrame(data)
print(shop)
shop["Total Sales"] = shop["Price"] * shop["Quantity"]
print(shop)
total_revenue = shop["Total Sales"].sum()
print("Total Revenue:", total_revenue)
best_selling = shop.loc[shop["Quantity"].idxmax()]
print("Best-Selling Product:")
print(best_selling)
plt.bar(shop["Product"], shop["Total Sales"])
plt.title("Total Sales by Product")
plt.xlabel("Product")
plt.ylabel("Sales in Naira")
plt.show()
plt.pie(
shop["Total Sales"],
labels=shop["Product"],
autopct="%1.1f%%"
)
plt.title("Contribution of Each Product to Total Sales")
plt.show()
After analysing the data and studying the charts, write a short report answering these questions:
Your completed project should contain:
In this module, you have learned that:
Well done! You have completed the Introduction to Python for Data Analysis course.
During this course, you learned how to:
Your next step: Continue practising Python with real-world datasets. Try analysing school results, shop sales, sports statistics, or other information that interests you.
Remember, becoming a good data analyst takes practice. Keep learning, keep experimenting, and keep asking questions!