Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to Python for Data Analysis ยท Lesson 9 of 9

Module Eight

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
9
Module 8: Data Visualisation and Practical Project This is the final module of *Introduction to Python for Data Analysis*. It teaches beginners how to turn data into charts, understand patterns, and present findings using Python. Module 8: Data Visualisation and Practical Project ```

Module 8: Data Visualisation and Practical Project

Introduction

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.

Learning Objectives

By the end of this module, you should be able to:

  • Explain what data visualisation means.
  • Understand why charts are useful in data analysis.
  • Identify different types of charts.
  • Install and import Matplotlib.
  • Create bar charts, line charts, and pie charts.
  • Add chart titles, labels, and legends.
  • Visualise data stored in a Pandas DataFrame.
  • Complete a simple Python data analysis project.

1. What Is Data Visualisation?

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.

2. Why Is Data Visualisation Important?

Data visualisation helps us to:

  • Understand large amounts of information quickly.
  • Compare different values.
  • Identify patterns and trends.
  • Discover unusual changes in data.
  • Explain findings to other people.
  • Make better decisions using data.

3. Common Types of Charts

A. Bar Chart

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.

B. Line Chart

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.

C. Pie Chart

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.

D. Histogram

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.

4. Introduction to Matplotlib

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.

5. Creating a Bar Chart

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?

  • Creates a list of subjects.
  • Stores the number of students for each subject.
  • Uses plt.bar() to create a bar chart.
  • Adds a title and labels to the chart.
  • Uses plt.show() to display the chart.

6. Creating a Line 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.

7. Creating a Pie Chart

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.

8. Creating a Histogram

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.

9. Adding More Details to Charts

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.

10. Visualising Data from Pandas

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.

11. Understanding What a Chart Tells You

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:

  • What information does the chart represent?
  • Which category has the highest value?
  • Which category has the lowest value?
  • Are the values increasing or decreasing?
  • Are there any unusual results?
  • What can we learn from the information?

12. Good Practices When Creating Charts

  • Choose a chart type that matches your data.
  • Give every chart a clear title.
  • Label the horizontal and vertical axes.
  • Use readable labels.
  • Avoid unnecessary decorations.
  • Do not hide important information.
  • Make sure the chart represents the data accurately.

13. Final Practical Project: Analyse a Shop's Sales

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.

Step 1: Create Your Dataset

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)

Step 2: Calculate Total Sales for Each Product

shop["Total Sales"] = shop["Price"] * shop["Quantity"]

print(shop)

Step 3: Calculate Total Revenue

total_revenue = shop["Total Sales"].sum()

print("Total Revenue:", total_revenue)

Step 4: Find the Best-Selling Product by Quantity

best_selling = shop.loc[shop["Quantity"].idxmax()]

print("Best-Selling Product:")
print(best_selling)

Step 5: Create a Bar Chart

plt.bar(shop["Product"], shop["Total Sales"])

plt.title("Total Sales by Product")
plt.xlabel("Product")
plt.ylabel("Sales in Naira")

plt.show()

Step 6: Create a Pie Chart

plt.pie(
    shop["Total Sales"],
    labels=shop["Product"],
    autopct="%1.1f%%"
)

plt.title("Contribution of Each Product to Total Sales")

plt.show()

Step 7: Write Your Findings

After analysing the data and studying the charts, write a short report answering these questions:

  • How much revenue did the shop generate?
  • Which product had the highest quantity sold?
  • Which product generated the highest sales value?
  • Which product generated the lowest sales value?
  • What should the shop owner consider doing next?

14. Project Submission Requirements

Your completed project should contain:

  1. A Python file containing your code.
  2. A Pandas DataFrame containing the sales records.
  3. A calculated total sales column.
  4. A bar chart showing sales by product.
  5. A pie chart showing each product's contribution to sales.
  6. A short written report explaining your findings.

15. Module Summary

In this module, you have learned that:

  • Data visualisation presents information using charts and graphs.
  • Matplotlib is a Python library for creating charts.
  • Bar charts compare categories.
  • Line charts show changes over time.
  • Pie charts show portions of a whole.
  • Histograms show the distribution of numerical data.
  • Pandas and Matplotlib can work together.
  • Charts help data analysts explain their findings.

16. Congratulations! You Have Completed the Course

Well done! You have completed the Introduction to Python for Data Analysis course.

During this course, you learned how to:

  • Understand Python and its role in data analysis.
  • Write basic Python programs.
  • Work with lists, tuples, dictionaries, and sets.
  • Use conditions, loops, and functions.
  • Understand NumPy, Pandas, and Matplotlib.
  • Organise and analyse data using Pandas.
  • Clean datasets and correct common data problems.
  • Create charts and visualise information.

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!

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’