PYTHON FOR AI • LESSON 4

Visualization Project

In this project, we will combine everything learned in Matplotlib to analyze a small student performance dataset. We will use line charts, bar charts, histograms, scatter plots, and subplots.

PROJECT GOAL

Turn raw data into useful visual information.

The goal is not simply to create charts. The goal is to use charts to understand the data and discover patterns. This is exactly why visualization is important in Data Science and Machine Learning.

01

Project Scenario

Imagine we have information about students and their performance.

For each student, we know:

  • Student name
  • Study hours
  • Exam score
  • Attendance percentage
  • Age

Our job is to visualize this information and answer simple questions such as:

  • How are students performing?
  • How are study hours distributed?
  • Do study hours relate to exam scores?
  • Does attendance relate to exam scores?
02

Create the Dataset

First, create some sample data using Python lists.

students = [
    "Amit",
    "John",
    "Sarah",
    "David",
    "Emma",
    "Raj",
    "Lisa",
    "Daniel"
]

study_hours = [
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
]

scores = [
    50,
    55,
    62,
    68,
    72,
    78,
    85,
    92
]

attendance = [
    70,
    75,
    78,
    82,
    85,
    88,
    92,
    95
]

ages = [
    18,
    19,
    18,
    20,
    19,
    21,
    20,
    19
]

Each position belongs to the same student.

Amit
study_hours → 2
score       → 50
attendance  → 70
age         → 18

The same relationship continues for the other students.

03

Import Matplotlib

We need Matplotlib to create our visualizations.

import matplotlib.pyplot as plt

We use plt as the short name for matplotlib.pyplot.

04

Visualization 1 — Line Chart

First, let's visualize the exam scores of the students.

plt.plot(
    students,
    scores,
    marker="o"
)

plt.title(
    "Student Exam Scores"
)

plt.xlabel(
    "Student"
)

plt.ylabel(
    "Score"
)

plt.xticks(
    rotation=45
)

plt.tight_layout()

plt.show()

The line chart helps us see how the scores change from student to student.

We can see that the scores generally increase across this sample dataset.

05

Visualization 2 — Bar Chart

A bar chart is useful when we want to compare individual values.

plt.bar(
    students,
    scores
)

plt.title(
    "Student Exam Scores"
)

plt.xlabel(
    "Student"
)

plt.ylabel(
    "Score"
)

plt.xticks(
    rotation=45
)

plt.tight_layout()

plt.show()

Here each bar represents one student's exam score.

Student
   ↓
Bar
   ↓
Exam Score

For comparing individual students, the bar chart is usually easier to read than the line chart.

06

Visualization 3 — Histogram

Now we want to understand how the exam scores are distributed.

This is different from comparing individual students.

plt.hist(
    scores,
    bins=5
)

plt.title(
    "Distribution of Exam Scores"
)

plt.xlabel(
    "Score"
)

plt.ylabel(
    "Number of Students"
)

plt.show()

A histogram groups numerical values into ranges.

Score range
    ↓
Number of students
    ↓
Distribution

This lets us see where most students' scores are concentrated.

07

Visualization 4 — Scatter Plot

Now we want to answer an important question:

Do students who study more tend to get higher scores?

This is a relationship between two numerical variables:

Study Hours
     ↓
     ?
     ↓
Exam Score

A scatter plot is appropriate for this.

plt.scatter(
    study_hours,
    scores
)

plt.title(
    "Study Hours vs Exam Score"
)

plt.xlabel(
    "Study Hours"
)

plt.ylabel(
    "Exam Score"
)

plt.show()

Each dot represents one student.

One student

Study Hours = 6
Score       = 72

becomes approximately:

(6, 72)

In this sample data, the points generally move upward, suggesting that more study hours are associated with higher scores.

Be careful with the wording: a scatter plot can show association, but it does not prove that studying more directly caused the higher score.

08

Analyze Another Relationship

We can also investigate attendance versus exam score.

plt.scatter(
    attendance,
    scores
)

plt.title(
    "Attendance vs Exam Score"
)

plt.xlabel(
    "Attendance (%)"
)

plt.ylabel(
    "Exam Score"
)

plt.show()

This lets us visually inspect whether students with higher attendance also tend to have higher scores.

09

Combine Everything Using Subplots

Creating five separate figures is not ideal when we want to analyze the dataset as a whole.

We can use subplots to put several visualizations into one figure.

fig, ax = plt.subplots(
    2,
    2,
    figsize=(12, 9)
)

This creates:

+----------------------+----------------------+
|                      |                      |
|      Chart 1         |      Chart 2         |
|                      |                      |
+----------------------+----------------------+
|                      |                      |
|      Chart 3         |      Chart 4         |
|                      |                      |
+----------------------+----------------------+
10

Build the Complete Visualization

Now we combine four different visualizations.

import matplotlib.pyplot as plt


students = [
    "Amit",
    "John",
    "Sarah",
    "David",
    "Emma",
    "Raj",
    "Lisa",
    "Daniel"
]

study_hours = [
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
]

scores = [
    50,
    55,
    62,
    68,
    72,
    78,
    85,
    92
]

attendance = [
    70,
    75,
    78,
    82,
    85,
    88,
    92,
    95
]

ages = [
    18,
    19,
    18,
    20,
    19,
    21,
    20,
    19
]


fig, ax = plt.subplots(
    2,
    2,
    figsize=(12, 9)
)


# --------------------------------
# 1. Bar Chart
# --------------------------------

ax[0, 0].bar(
    students,
    scores
)

ax[0, 0].set_title(
    "Exam Scores"
)

ax[0, 0].set_xlabel(
    "Student"
)

ax[0, 0].set_ylabel(
    "Score"
)

ax[0, 0].tick_params(
    axis="x",
    rotation=45
)


# --------------------------------
# 2. Histogram
# --------------------------------

ax[0, 1].hist(
    scores,
    bins=5
)

ax[0, 1].set_title(
    "Score Distribution"
)

ax[0, 1].set_xlabel(
    "Score"
)

ax[0, 1].set_ylabel(
    "Number of Students"
)


# --------------------------------
# 3. Study Hours vs Score
# --------------------------------

ax[1, 0].scatter(
    study_hours,
    scores
)

ax[1, 0].set_title(
    "Study Hours vs Score"
)

ax[1, 0].set_xlabel(
    "Study Hours"
)

ax[1, 0].set_ylabel(
    "Score"
)


# --------------------------------
# 4. Attendance vs Score
# --------------------------------

ax[1, 1].scatter(
    attendance,
    scores
)

ax[1, 1].set_title(
    "Attendance vs Score"
)

ax[1, 1].set_xlabel(
    "Attendance (%)"
)

ax[1, 1].set_ylabel(
    "Score"
)


plt.tight_layout()

plt.show()

Now we have four different views of the same dataset.

                    Student Dataset
                          │
             ┌────────────┼────────────┐
             ↓            ↓            ↓
          Scores    Study Hours    Attendance
             │            │            │
             ↓            ↓            ↓
           Bar         Scatter       Scatter
             │
             ↓
        Distribution
             │
             ↓
         Histogram
11

How to Read the Results

A visualization project is not complete just because the charts appear on the screen.

You should ask questions about the charts.

Question 1 — What is the score distribution?

Look at the histogram. It shows how the exam scores are distributed across different score ranges.

Question 2 — Is there a relationship between study hours and score?

Look at the scatter plot. In our sample data, the points generally increase as study hours increase.

Question 3 — Is attendance related to score?

Look at the second scatter plot. The sample data also shows an upward relationship between attendance and scores.

These are observations from the visualization, not proof of causation.

12

Why This Matters in AI

Visualization is not just for making data look nice.

Before training a Machine Learning model, you need to understand your data.

Raw Dataset
     ↓
Explore Data
     ↓
Visualize Data
     ↓
Find Patterns
     ↓
Find Outliers
     ↓
Understand Relationships
     ↓
Prepare Features
     ↓
Train ML Model

For example, a scatter plot might reveal that one feature has almost no relationship with the target. A histogram might reveal unusual values. A bar chart might reveal an imbalance between categories.

These observations can influence how you prepare the dataset before Machine Learning.

13

Complete Project Code

Here is the complete version you can run as one Python file.

import matplotlib.pyplot as plt


# --------------------------------
# Dataset
# --------------------------------

students = [
    "Amit",
    "John",
    "Sarah",
    "David",
    "Emma",
    "Raj",
    "Lisa",
    "Daniel"
]

study_hours = [
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
]

scores = [
    50,
    55,
    62,
    68,
    72,
    78,
    85,
    92
]

attendance = [
    70,
    75,
    78,
    82,
    85,
    88,
    92,
    95
]

ages = [
    18,
    19,
    18,
    20,
    19,
    21,
    20,
    19
]


# --------------------------------
# Create figure
# --------------------------------

fig, ax = plt.subplots(
    2,
    2,
    figsize=(12, 9)
)


# --------------------------------
# Bar Chart
# --------------------------------

ax[0, 0].bar(
    students,
    scores
)

ax[0, 0].set_title(
    "Exam Scores"
)

ax[0, 0].set_xlabel(
    "Student"
)

ax[0, 0].set_ylabel(
    "Score"
)

ax[0, 0].tick_params(
    axis="x",
    rotation=45
)


# --------------------------------
# Histogram
# --------------------------------

ax[0, 1].hist(
    scores,
    bins=5
)

ax[0, 1].set_title(
    "Score Distribution"
)

ax[0, 1].set_xlabel(
    "Score"
)

ax[0, 1].set_ylabel(
    "Number of Students"
)


# --------------------------------
# Study Hours vs Score
# --------------------------------

ax[1, 0].scatter(
    study_hours,
    scores
)

ax[1, 0].set_title(
    "Study Hours vs Score"
)

ax[1, 0].set_xlabel(
    "Study Hours"
)

ax[1, 0].set_ylabel(
    "Score"
)


# --------------------------------
# Attendance vs Score
# --------------------------------

ax[1, 1].scatter(
    attendance,
    scores
)

ax[1, 1].set_title(
    "Attendance vs Score"
)

ax[1, 1].set_xlabel(
    "Attendance (%)"
)

ax[1, 1].set_ylabel(
    "Score"
)


# --------------------------------
# Improve layout
# --------------------------------

plt.tight_layout()

plt.show()
14

Practice Challenge

Now modify the project yourself.

Add a new visualization showing the relationship between age and exam score.

Use a scatter plot.

ax[1, 1].scatter(
    ages,
    scores
)

ax[1, 1].set_title(
    "Age vs Score"
)

ax[1, 1].set_xlabel(
    "Age"
)

ax[1, 1].set_ylabel(
    "Score"
)

Then ask yourself: does age appear to have a strong relationship with exam score in this small dataset?

There is no need to force a relationship. If the data does not show a clear pattern, that is a valid finding.

15

What You Learned

In this project, you used all the major visualization concepts from this lesson.

Line Chart
    ↓
See changes and trends

Bar Chart
    ↓
Compare categories

Histogram
    ↓
Understand distribution

Scatter Plot
    ↓
Study relationships

Subplots
    ↓
Compare multiple visualizations

Complete Project
    ↓
Use them together
KEY TAKEAWAY

Visualization is about understanding data, not just drawing charts.

A good Data Scientist does not create charts randomly. They choose a visualization based on the question they want to answer. Use line charts for trends, bar charts for comparisons, histograms for distributions, scatter plots for relationships, and subplots when several views need to be compared together. This is the foundation you will use later when exploring real datasets and preparing data for Machine Learning.