PYTHON FOR AI • LESSON 4

Histograms

A histogram helps you understand how numerical values are distributed by grouping values into ranges and showing how many values fall inside each range.

CORE IDEA

A histogram shows the distribution of numerical data.

Instead of looking at every individual number, a histogram groups numbers into ranges and counts how many values belong to each range.

01

What Is a Histogram?

Imagine you have the ages of 20 people.

18, 19, 21, 22, 23,
24, 25, 25, 26, 27,
28, 29, 30, 31, 32,
33, 35, 36, 38, 40

Looking at these numbers individually does not tell us much about the overall distribution.

A histogram groups them into ranges.

18 - 22  →  4 people
23 - 27  →  6 people
28 - 32  →  5 people
33 - 37  →  3 people
38 - 42  →  2 people

The histogram then displays those counts visually.

02

What Are Bins?

The ranges used to group values are called bins.

For example:

Bin 1 → 0 - 10
Bin 2 → 10 - 20
Bin 3 → 20 - 30
Bin 4 → 30 - 40

Each bin counts how many data points fall into that range.

Simple way to remember

Numbers
   ↓
Divide into ranges
   ↓
Count values in each range
   ↓
Display the counts
   ↓
Histogram
03

Histogram vs Bar Chart

This is one of the most important differences to understand.

Bar chart

Product A → 500
Product B → 800
Product C → 300

These are separate categories. You are comparing categories.

Histogram

10 - 20 → 5 values
20 - 30 → 12 values
30 - 40 → 8 values

These are numerical ranges. You are studying how values are distributed.

BAR CHART
    ↓
Compare categories

HISTOGRAM
    ↓
Understand numerical distribution
04

Import Matplotlib

We use Matplotlib to create the histogram.

import matplotlib.pyplot as plt
05

Create Your First Histogram

Matplotlib provides plt.hist() for creating histograms.

import matplotlib.pyplot as plt

ages = [
    18, 19, 21, 22, 23,
    24, 25, 25, 26, 27,
    28, 29, 30, 31, 32,
    33, 35, 36, 38, 40
]

plt.hist(ages)

plt.show()

That's enough to create a basic histogram.

Matplotlib automatically divides the numerical values into bins and counts the values in those bins.

06

Control the Number of Bins

You can control how many bins the histogram uses.

plt.hist(
    ages,
    bins=5
)

plt.show()

Here:

bins=5

means that we are asking Matplotlib to divide the data into approximately five ranges.

The number of bins affects how much detail you see.

Too few bins

Important patterns may disappear because too many values are grouped together.

Too many bins

The histogram can become noisy and difficult to interpret.

07

Add a Title and Labels

Just like with line and bar charts, we should make the histogram understandable.

import matplotlib.pyplot as plt

ages = [
    18, 19, 21, 22, 23,
    24, 25, 25, 26, 27,
    28, 29, 30, 31, 32,
    33, 35, 36, 38, 40
]

plt.hist(
    ages,
    bins=5
)

plt.title("Age Distribution")

plt.xlabel("Age")

plt.ylabel("Number of People")

plt.show()

Now the chart clearly communicates:

X-axis
   ↓
Age ranges

Y-axis
   ↓
Number of people

Chart
   ↓
Age distribution
08

Show the Edges of the Bars

You can add an edge around each bin to make the boundaries easier to see.

plt.hist(
    ages,
    bins=5,
    edgecolor="black"
)

plt.show()

The important part is:

edgecolor="black"

This makes the boundary between neighboring bins easier to distinguish.

09

Understand the Distribution

The main reason we use histograms is to understand the shape of the data.

For example, suppose we have:

scores = [
    45, 50, 52, 55, 58,
    60, 61, 62, 64, 65,
    66, 67, 68, 70, 72,
    75, 78, 80, 85, 90
]

A histogram helps us see where most scores are located.

Score range     Number of students

40 - 50         ███
50 - 60         ████
60 - 70         ███████
70 - 80         ████
80 - 90         ███

We can now see that many values are concentrated around the middle ranges.

10

Real-World Example: Exam Scores

Imagine a teacher wants to understand how students performed in an exam.

import matplotlib.pyplot as plt

scores = [
    45, 50, 52, 55, 58,
    60, 61, 62, 64, 65,
    66, 67, 68, 70, 72,
    75, 78, 80, 85, 90
]

plt.hist(
    scores,
    bins=5,
    edgecolor="black"
)

plt.title("Exam Score Distribution")

plt.xlabel("Score")

plt.ylabel("Number of Students")

plt.show()

The teacher is not mainly asking which individual student scored highest.

The question is:

Where are most students' scores concentrated?

That is exactly the kind of question a histogram helps answer.

11

Histograms in AI

Histograms are very useful in Machine Learning because they help us understand the distribution of features.

For example, suppose a dataset contains the age of customers.

import matplotlib.pyplot as plt

ages = [
    18, 20, 21, 22, 24,
    25, 25, 27, 28, 30,
    31, 32, 34, 35, 36,
    38, 40, 42, 45, 50
]

plt.hist(
    ages,
    bins=6,
    edgecolor="black"
)

plt.title("Customer Age Distribution")

plt.xlabel("Age")

plt.ylabel("Number of Customers")

plt.show()

Before building an ML model, understanding the distribution of your data is useful.

For example, you might discover that most customers are between 20 and 40 years old.

12

Histogram of a Machine Learning Feature

Suppose you have a feature called income.

income = [
    25000,
    28000,
    30000,
    32000,
    35000,
    38000,
    40000,
    45000,
    50000,
    55000,
    60000,
    70000,
    90000,
    120000
]

You can visualize its distribution:

plt.hist(
    income,
    bins=7,
    edgecolor="black"
)

plt.title("Income Distribution")

plt.xlabel("Income")

plt.ylabel("Frequency")

plt.show()

This can reveal whether the data is concentrated in a particular range or whether some values are much larger than most other values.

13

Histograms Can Help Spot Outliers

An outlier is a value that is unusually far away from most of the other values.

ages = [
    20, 21, 22, 23, 24,
    25, 26, 27, 28, 29,
    30, 31, 32, 33, 95
]

Most values are around 20–33, but there is a value of 95.

A histogram can make that unusual value visible.

plt.hist(
    ages,
    bins=8,
    edgecolor="black"
)

plt.title("Age Distribution")

plt.xlabel("Age")

plt.ylabel("Frequency")

plt.show()

This does not automatically mean that 95 is wrong. It only tells you that the value deserves investigation.

14

Complete Histogram Example

Here is a clean example combining the main concepts.

import matplotlib.pyplot as plt

scores = [
    45, 50, 52, 55, 58,
    60, 61, 62, 64, 65,
    66, 67, 68, 70, 72,
    75, 78, 80, 85, 90
]

plt.hist(
    scores,
    bins=5,
    edgecolor="black"
)

plt.title("Exam Score Distribution")

plt.xlabel("Score")

plt.ylabel("Number of Students")

plt.grid(axis="y")

plt.show()

The workflow is:

Collect numerical data
        ↓
Choose bins
        ↓
plt.hist()
        ↓
Count values in ranges
        ↓
Add title and labels
        ↓
Study the distribution
15

The Most Important Rule

Do not confuse a histogram with a bar chart.

Bar chart

India  → 1200
USA    → 2500
Japan  → 1400

These are separate categories. You are comparing countries.

Histogram

0 - 10   → 5 values
10 - 20  → 12 values
20 - 30  → 8 values
30 - 40  → 3 values

These are numerical ranges. You are studying how values are distributed.

KEY TAKEAWAY

Histograms help you understand data distribution.

Use plt.hist() when you want to understand how numerical values are spread across ranges. The ranges are called bins, and the height of each bin represents how many values fall inside that range. Histograms are especially useful in AI and Machine Learning for exploring numerical features, identifying concentration of values, and spotting unusual values.