PYTHON FOR AI • LESSON 4

Scatter Plots

A scatter plot is used to understand the relationship between two numerical variables. Each point represents one observation.

CORE IDEA

Scatter plots help you see relationships between two numbers.

For example, you can compare study hours with exam scores, advertising spend with sales, or house size with house price.

01

What Is a Scatter Plot?

A scatter plot displays individual data points using two numerical variables.

For example, suppose we have:

Study Hours    Exam Score

2              50
3              55
4              60
5              68
6              72
7              80
8              88

Each student becomes one point on the chart.

       Exam Score
            ↑
        90  |
        80  |                •
        70  |          •  •
        60  |      •
        50  |  •  •
            +--------------------→
               Study Hours
02

X-Axis and Y-Axis

A scatter plot normally has two variables.

X-axis
  ↓
First numerical variable

Y-axis
  ↓
Second numerical variable

For our example:

X = Study Hours

Y = Exam Score

So the point:

(5, 68)

means:

5 hours of study
        ↓
68 exam score
03

Create Your First Scatter Plot

Matplotlib provides plt.scatter() for creating scatter plots.

import matplotlib.pyplot as plt

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

exam_scores = [
    50, 55, 60, 68, 72, 80, 88
]

plt.scatter(
    study_hours,
    exam_scores
)

plt.show()

The first list becomes the X-axis and the second list becomes the Y-axis.

plt.scatter(
    x_values,
    y_values
)
04

Add a Title and Labels

A chart should explain what the two axes represent.

import matplotlib.pyplot as plt

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

exam_scores = [
    50, 55, 60, 68, 72, 80, 88
]

plt.scatter(
    study_hours,
    exam_scores
)

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

plt.xlabel("Study Hours")

plt.ylabel("Exam Score")

plt.show()

Now someone looking at the chart immediately knows what the points represent.

05

Understand the Relationship

This is the main reason we use scatter plots.

Look at where the points are located.

Positive relationship

Study Hours ↑
      ↓
Exam Score ↑

If study hours increase and exam scores generally increase, the variables have a positive relationship.

Negative relationship

Price ↑
   ↓
Demand ↓

If one variable increases while the other generally decreases, the relationship is negative.

06

No Clear Relationship

Sometimes the points are scattered randomly.

x = [
    1, 2, 3, 4, 5, 6, 7
]

y = [
    72, 45, 80, 51, 63, 40, 75
]

plt.scatter(x, y)

plt.show()

If there is no obvious upward or downward pattern, there may be little or no relationship between the variables.

Positive       Negative       No clear pattern

   •              • •             •    •
    •            •                •
      •        •                    •  •
        •    •                  •
          •                  •       •
07

Add a Trend Line

A trend line can help us see the general direction of a relationship.

One simple way to calculate a trend line is with NumPy's polyfit().

import numpy as np
import matplotlib.pyplot as plt

study_hours = np.array([
    2, 3, 4, 5, 6, 7, 8
])

exam_scores = np.array([
    50, 55, 60, 68, 72, 80, 88
])

plt.scatter(
    study_hours,
    exam_scores
)

m, b = np.polyfit(
    study_hours,
    exam_scores,
    1
)

plt.plot(
    study_hours,
    m * study_hours + b
)

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

plt.show()

Here:

np.polyfit(..., 1)
        ↓
Find a straight-line relationship

m
↓
Slope

b
↓
Intercept

You don't need to memorize the mathematics yet. The important idea is that the line summarizes the general direction of the points.

08

Customize the Points

You can change the size and transparency of the points.

plt.scatter(
    study_hours,
    exam_scores,
    s=80,
    alpha=0.7
)

plt.show()

Here:

s=80
 ↓
Point size

alpha=0.7
 ↓
Transparency
09

Compare Two Groups

Scatter plots can also compare multiple groups.

import matplotlib.pyplot as plt

hours_group_1 = [2, 3, 4, 5, 6]
scores_group_1 = [50, 55, 62, 68, 72]

hours_group_2 = [3, 4, 5, 6, 7]
scores_group_2 = [60, 68, 75, 82, 90]

plt.scatter(
    hours_group_1,
    scores_group_1,
    label="Group 1"
)

plt.scatter(
    hours_group_2,
    scores_group_2,
    label="Group 2"
)

plt.xlabel("Study Hours")
plt.ylabel("Exam Score")

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

plt.legend()

plt.show()

The label values are displayed using plt.legend().

10

Real-World Example: Advertising and Sales

Suppose a company wants to understand whether spending more money on advertising is associated with higher sales.

import matplotlib.pyplot as plt

ad_spend = [
    10, 15, 20, 25, 30,
    35, 40, 45, 50
]

sales = [
    100, 120, 135, 150, 170,
    190, 210, 230, 250
]

plt.scatter(
    ad_spend,
    sales
)

plt.title("Advertising Spend vs Sales")

plt.xlabel("Advertising Spend")

plt.ylabel("Sales")

plt.show()

Each point represents one observation.

(10, 100)
(15, 120)
(20, 135)
(25, 150)
...

The points generally move upward, suggesting a positive relationship in this example.

11

Scatter Plots in AI and Machine Learning

Scatter plots are extremely useful during Exploratory Data Analysis (EDA).

For example, suppose a dataset contains:

House Size
    +
House Price

You can visualize whether larger houses tend to have higher prices.

import matplotlib.pyplot as plt

house_size = [
    800,
    1000,
    1200,
    1500,
    1800,
    2200,
    2500
]

house_price = [
    150000,
    180000,
    220000,
    280000,
    340000,
    410000,
    470000
]

plt.scatter(
    house_size,
    house_price
)

plt.title("House Size vs House Price")

plt.xlabel("House Size (sq ft)")

plt.ylabel("House Price")

plt.show()

If the points generally move upward, house size and house price have a positive relationship in this example.

12

Scatter Plot With Pandas

In real Data Science projects, the data usually comes from a DataFrame.

import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({
    "study_hours": [
        2, 3, 4, 5, 6, 7, 8
    ],
    "exam_score": [
        50, 55, 60, 68, 72, 80, 88
    ]
})

plt.scatter(
    data["study_hours"],
    data["exam_score"]
)

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

plt.xlabel("Study Hours")

plt.ylabel("Exam Score")

plt.show()

The workflow is:

Pandas
  ↓
Load / clean / prepare data
  ↓
Matplotlib
  ↓
Visualize relationship
13

Scatter Plots Can Reveal Outliers

Scatter plots can make unusual observations easy to notice.

study_hours = [
    2, 3, 4, 5, 6, 7, 20
]

exam_scores = [
    50, 55, 60, 68, 72, 80, 52
]

plt.scatter(
    study_hours,
    exam_scores
)

plt.xlabel("Study Hours")
plt.ylabel("Exam Score")

plt.show()

Most points follow one general pattern, but the point representing 20 study hours and a score of 52 is unusual.

That doesn't automatically mean the data is wrong. It means you should investigate that observation.

14

Scatter Plot and Correlation

Correlation measures how strongly two numerical variables move together.

Positive correlation
        ↗
      •
    •
  •
•


Negative correlation
\
  •
    •
      •
        •


No strong correlation

•    •
   •
      •
 •      •

A scatter plot lets you visually inspect this relationship before using a statistical measure such as correlation.

Important: correlation does not prove causation.

Example

If advertising spend and sales are positively correlated, that does not automatically prove that advertising caused every increase in sales.

15

Complete Scatter Plot Example

Here is a clean example combining the main concepts.

import matplotlib.pyplot as plt

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

exam_scores = [
    50, 55, 60, 68, 72, 80, 88
]

plt.scatter(
    study_hours,
    exam_scores,
    s=80,
    alpha=0.7
)

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

plt.xlabel(
    "Study Hours"
)

plt.ylabel(
    "Exam Score"
)

plt.grid(True)

plt.show()

The workflow is:

Choose two numerical variables
          ↓
X = first variable
          ↓
Y = second variable
          ↓
plt.scatter(X, Y)
          ↓
Each row becomes one point
          ↓
Look for patterns
          ↓
Understand the relationship
16

The Most Important Rule

Don't use a scatter plot simply because you have two columns.

Use it when both variables are numerical and you want to understand their relationship.

Good use

Study Hours ↔ Exam Score

Height ↔ Weight

House Size ↔ House Price

Ad Spend ↔ Sales

Both variables are numerical and their relationship is meaningful.

Better choice: Bar Chart

India  → 1200
USA    → 2500
Japan  → 1400

Countries are categories, so a bar chart is more appropriate.

KEY TAKEAWAY

Scatter plots show relationships between two numerical variables.

Use plt.scatter() when you want to see how two numerical variables relate to each other. Each observation becomes one point. The pattern of the points can help you identify positive relationships, negative relationships, weak relationships, and unusual values. Scatter plots are especially important in AI and Machine Learning during Exploratory Data Analysis.