0
0
Matplotlibdata~5 mins

Why histograms show distributions in Matplotlib

Choose your learning style9 modes available
Introduction

Histograms help us see how data spreads out. They show how often values appear in groups.

To understand the range and common values in a dataset, like exam scores.
To check if data is balanced or skewed, like ages of customers.
To compare distributions between groups, like sales in different months.
To find unusual values or outliers in data, like very high or low temperatures.
Syntax
Matplotlib
matplotlib.pyplot.hist(data, bins=number_of_bins, color='color_name', edgecolor='color_name')

data is the list or array of numbers you want to plot.

bins controls how many groups the data is split into.

Examples
Basic histogram with 5 bins showing frequency of values.
Matplotlib
import matplotlib.pyplot as plt

values = [1, 2, 2, 3, 3, 3, 4, 4, 5]
plt.hist(values, bins=5)
plt.show()
Histogram with 3 bins and colors for better look.
Matplotlib
plt.hist(values, bins=3, color='skyblue', edgecolor='black')
plt.show()
Sample Program

This program shows how ages are spread across groups. The histogram bars show how many people fall into each age range.

Matplotlib
import matplotlib.pyplot as plt

# Sample data: ages of 20 people
ages = [22, 25, 27, 30, 22, 24, 29, 31, 35, 40, 42, 45, 50, 55, 60, 65, 70, 75, 80, 85]

# Create histogram with 8 bins
plt.hist(ages, bins=8, color='green', edgecolor='black')
plt.title('Age Distribution')
plt.xlabel('Age groups')
plt.ylabel('Number of people')
plt.show()
OutputSuccess
Important Notes

More bins give detailed view but can be noisy.

Fewer bins give a simpler overview but may hide details.

Histograms show counts, not exact values.

Summary

Histograms group data into bins to show frequency.

They help us understand data spread and common values.

Adjusting bins changes the detail level of the distribution.