0
0
Matplotlibdata~10 mins

Normalized histograms in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Normalized histograms
Start with data array
Choose number of bins
Calculate frequency counts per bin
Normalize counts by total data points and bin width
Plot histogram with normalized heights
Show histogram where area sums to 1
This flow shows how data is divided into bins, counts are normalized to represent probabilities, and then plotted as a histogram.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
plt.hist(data, bins=30, density=True)
plt.show()
This code creates a normalized histogram of 1000 random numbers from a normal distribution.
Execution Table
StepActionData/VariableResult/Output
1Generate datadata = 1000 random valuesArray of 1000 floats
2Set binsbins = 30Divide data range into 30 intervals
3Count data in binscounts per binArray of counts, sum ~1000
4Normalize countsdensity=TrueCounts divided by (total points * bin width)
5Plot histogramplt.hist(..., density=True)Bar heights sum to 1 area
6Show plotplt.show()Histogram displayed on screen
💡 All steps complete, histogram normalized and displayed
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
dataNoneArray of 1000 floatsSameSameSame
binsNoneNone30 intervalsSameSame
countsNoneNoneCounts per bin (~1000 total)Normalized countsUsed for plotting
Key Moments - 2 Insights
Why does setting density=True change the histogram heights?
Setting density=True divides counts by total points and bin width, so the histogram shows probability density instead of raw counts, as seen in execution_table step 4.
What does the area under the normalized histogram represent?
The area sums to 1, representing the total probability of the data distribution, confirmed in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the 'counts per bin' represent at step 3?
AThe number of data points in each bin
BThe total number of bins
CThe normalized frequency of data
DThe bin width
💡 Hint
Check the 'Data/Variable' column at step 3 in execution_table
At which step does the histogram get normalized so that the area sums to 1?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look for 'Normalize counts' and 'density=True' in execution_table
If we increase the number of bins, how would the 'counts per bin' change at step 3?
ACounts per bin would increase
BCounts per bin would decrease
CCounts per bin would stay the same
DCounts per bin would become negative
💡 Hint
More bins means data is split into smaller intervals, so fewer points per bin
Concept Snapshot
Normalized histograms show data distribution as probabilities.
Use plt.hist(data, bins, density=True) to normalize.
Counts are divided by total points and bin width.
Area under histogram sums to 1.
Useful to compare distributions regardless of sample size.
Full Transcript
This lesson shows how to create normalized histograms using matplotlib. First, data is generated or given. Then, the data range is divided into bins. We count how many data points fall into each bin. To normalize, counts are divided by the total number of points and the bin width, so the histogram represents a probability density. Finally, the histogram is plotted with bars whose heights correspond to these normalized values. The area under the histogram sums to 1, representing the total probability. This helps compare different datasets fairly. The execution table traces each step from data generation to plotting. Key moments clarify why normalization changes bar heights and what the area means. The quiz tests understanding of counts, normalization step, and effect of bin number.