Learn Histograms in Python with Matplotlib – Step-by-Step with Code

A histogram is like a bar chart’s nerdy cousin — it doesn’t just compare things, it shows how your numbers are spread out. Instead of labels like "Apples" or "Oranges," you get ranges like 0–10, 10–20, and so on. It’s perfect when you want to see patterns in things like test scores, survey results, or even the ages of your favorite superheroes.

When should you use a histogram?

Use a histogram when you want to understand how your data is spread out — like figuring out what range most test scores fall into, or how often certain temperatures occur. It’s your go-to graph for spotting trends, clusters, or outliers in numeric data. If your data is continuous (like age, marks, height, time), a histogram is way more helpful than a pie chart or line graph.

Real-Life Examples of Histograms



Step-by-Step Code Example

Here’s a simple example of how to create a histogram graph in Matplotlib:

Python

import matplotlib.pyplot as plt

# Sample data: Ages of 200 people
ages = [22, 25, 29, 30, 32, 33, 35, 36, 38, 40,
        42, 44, 45, 48, 50, 52, 55, 58, 60, 65] * 10

# Create the histogram
plt.hist(ages, bins=8, color='skyblue', edgecolor='black')

# Add titles and labels
plt.title("Age Distribution of Survey Participants")
plt.xlabel("Age Groups")
plt.ylabel("Number of People")

# Show the plot
plt.show()

        

Understanding the Code Step-by-Step

Let’s break down what’s happening in our histogram code, one line at a time — no boring jargon, just clear logic!

🔹import matplotlib.pyplot as plt

We’re grabbing Matplotlib’s plotting tools. Think of this like opening your paintbox before making a graph.

🔹ages = [22, 25, 29, ..., 65] * 10

Our sample data: a list of ages repeated 10 times. It mimics a group of 200 people for better visualization.

🔹plt.hist(ages, bins=8, color='skyblue', edgecolor='black')

This is where the histogram is made:
ages → the data we’re visualizing
bins=8 → groups data into 8 age ranges
color and edgecolor make it neat and readable

🔹plt.title("Age Distribution") plt.xlabel("Age Groups") plt.ylabel("Number of People")

We’re adding context: a title and axis labels so the chart makes perfect sense at a glance.

🔹plt.show()

This line says: “All set — now show me the chart!”


Output of the above code :


Output


Customize Your Histogram


Histograms don’t have to be boring! You can tweak colors, borders, transparency, and even the style of bars to make your data look clear and beautiful. Here’s how you can style it up:

Python

import matplotlib.pyplot as plt

ages = [21, 23, 25, 22, 26, 29, 21, 25, 27, 30, 24, 23, 22, 28, 26]

plt.hist(
    ages,
    bins=8,                       # Number of bars
    color='#4db6ac',              # Soft teal fill color
    edgecolor='white',            # White borders between bars
    linewidth=1.2,                # Border thickness
    alpha=0.85,                   # Slight transparency
    histtype='bar'                # Try 'step' or 'barstacked' too!
)

plt.title("Age Distribution of Classmates")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.grid(True, linestyle='--', alpha=0.4)
plt.show()

        

What’s Happening Here?

Go ahead and play with these — tweak the colors, bin count, and styles to make your graph match your vibe.



Output of the above code :


Output 2


Mini Project: Visualize Exam Score Distribution

Let’s say you’ve got the exam scores of 50 students. Build a histogram to visualize how those scores are spread out — who’s topping, who’s in the middle, and how many are lagging behind.

What You’ll Do:

Bonus: Try grouping scores like 0–20, 21–40, etc., and label them smartly!

Perfect for teachers, students analyzing trends, or just for Python practice.





More educational content :