0
0
Matplotlibdata~5 mins

Normalized histograms in Matplotlib

Choose your learning style9 modes available
Introduction

Normalized histograms show the shape of data by scaling the counts so the total area equals 1. This helps compare different data sets fairly.

Comparing the distribution of test scores from two different classes with different numbers of students.
Visualizing the probability distribution of daily temperatures over a month.
Checking the shape of sales data from two stores with different customer counts.
Understanding the relative frequency of different categories in survey results.
Syntax
Matplotlib
plt.hist(data, bins=number_of_bins, density=True)

The density=True option scales the histogram so the area sums to 1.

You can adjust bins to control how detailed the histogram is.

Examples
Creates a normalized histogram with 10 bins.
Matplotlib
plt.hist(data, bins=10, density=True)
Creates a normalized histogram with default bin count.
Matplotlib
plt.hist(data, density=True)
Creates a regular histogram with counts, not normalized.
Matplotlib
plt.hist(data, bins=20, density=False)
Sample Program

This code creates 1000 random numbers from a normal distribution and plots their normalized histogram with 30 bins. The y-axis shows probability density, so the total area under the bars equals 1.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Generate random data from a normal distribution
np.random.seed(0)
data = np.random.randn(1000)

# Plot normalized histogram
plt.hist(data, bins=30, density=True, alpha=0.7, color='blue')
plt.title('Normalized Histogram of Random Data')
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Normalized histograms are useful to compare data sets of different sizes.

The density=True option changes the y-axis to show probability density instead of counts.

Always label your axes to make the plot clear.

Summary

Normalized histograms scale counts so the total area equals 1.

Use density=True in plt.hist() to normalize.

They help compare distributions fairly regardless of sample size.