0
0
Matplotlibdata~10 mins

Multiple histograms overlay in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple histograms overlay
Prepare data sets
Call plt.hist() for each data set with alpha
Histograms drawn on same plot
Add labels, legend, title
Show combined histogram plot
We prepare multiple data sets, then draw their histograms on the same plot with transparency to see overlaps.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Two data sets
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1.5, 1000)

plt.hist(data1, bins=30, alpha=0.5, label='Data 1')
plt.hist(data2, bins=30, alpha=0.5, label='Data 2')
plt.legend()
plt.show()
This code draws two histograms on the same plot with some transparency so we can see where they overlap.
Execution Table
StepActionData UsedBinsAlphaPlot StateOutput
1Generate data1Normal(0,1), 1000 points--No plot yetdata1 array created
2Generate data2Normal(2,1.5), 1000 points--No plot yetdata2 array created
3Plot histogram for data1data1300.5Plot with data1 histogramHistogram bars for data1 shown with 50% transparency
4Plot histogram for data2data2300.5Plot with data1 and data2 histogramsHistogram bars for data2 shown overlapping data1 with 50% transparency
5Add legend---Plot with legendLegend shows 'Data 1' and 'Data 2' labels
6Show plot---Plot displayedCombined histogram plot visible with overlays
💡 All steps complete, plot shown with both histograms overlayed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
data1NoneArray of 1000 values ~ N(0,1)Array unchangedArray unchangedArray unchangedArray unchanged
data2NoneNoneArray of 1000 values ~ N(2,1.5)Array unchangedArray unchangedArray unchanged
plot_stateEmptyEmptyEmptyHistogram of data1 drawnHistograms of data1 and data2 drawnPlot with legend and shown
Key Moments - 3 Insights
Why do we use alpha=0.5 when plotting multiple histograms?
Alpha sets transparency. Using 0.5 lets us see overlapping bars clearly, as shown in execution_table steps 3 and 4.
What happens if we don't call plt.legend() after plotting?
Without plt.legend(), the plot won't show labels for each histogram, making it hard to tell which bars belong to which data set (see step 5).
Why do both histograms use the same number of bins?
Using the same bins (30 here) aligns the bars for easy comparison, as seen in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What does the plot state show after plotting data2?
APlot with data1 and data2 histograms overlayed
BPlot with only data2 histogram
CEmpty plot with no histograms
DPlot with legend only
💡 Hint
Check the 'Plot State' column at step 4 in execution_table.
At which step is the legend added to the plot?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the action 'Add legend' in execution_table.
If we remove alpha=0.5 from both plt.hist calls, what will happen to the plot?
AHistograms will be transparent and easy to compare
BPlot will show no histograms
CHistograms will be fully opaque and overlapping bars may hide each other
DPlot will show only one histogram
💡 Hint
Refer to key_moments about the role of alpha transparency.
Concept Snapshot
Multiple histograms overlay:
- Use plt.hist() multiple times on same plot
- Use same bins for alignment
- Use alpha (0-1) for transparency
- Add labels and plt.legend() for clarity
- Call plt.show() to display combined plot
Full Transcript
This visual execution shows how to overlay multiple histograms using matplotlib. First, two data sets are created with different normal distributions. Then, plt.hist() is called twice with the same number of bins and alpha=0.5 for transparency. This allows both histograms to appear on the same plot, showing overlaps clearly. A legend is added to label each histogram. Finally, plt.show() displays the combined plot. Key points include using alpha for transparency and adding a legend for clarity.