0
0
Matplotlibdata~20 mins

Multiple histograms overlay in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Histogram Overlay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overlaying two histograms with transparency
What is the output of this code that overlays two histograms with alpha transparency?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1, 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()
ATwo overlapping histograms with semi-transparent bars, one centered near 0 and the other near 2, both visible with legend labels.
BA single histogram combining both datasets into one, centered near 1 with no transparency or legend.
CTwo histograms plotted side by side without overlap, each with full opacity and no transparency.
DAn empty plot with no bars visible because alpha is set to 0.
Attempts:
2 left
💡 Hint
Look at the alpha parameter controlling transparency and how plt.hist overlays multiple calls.
data_output
intermediate
1:30remaining
Number of bars in overlaid histograms
Given two datasets each with 1000 points, and histograms plotted with bins=20 overlaid, how many bars are visible in total?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(1, 1, 1000)

plt.hist(data1, bins=20, alpha=0.6)
plt.hist(data2, bins=20, alpha=0.6)
plt.show()
A40 bars total, 20 bars for each histogram overlaid.
B20 bars total, because the histograms overlap on the same bins.
C10 bars total, because bins are shared and combined.
DNo bars visible because alpha is less than 1.
Attempts:
2 left
💡 Hint
Each plt.hist call creates its own set of bars even if bins are the same.
🔧 Debug
advanced
2:00remaining
Why does this overlaid histogram code show only one dataset?
This code intends to overlay two histograms but only shows one. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(3, 1, 1000)

plt.hist(data1, bins=25, alpha=0.7, label='Data 1')
plt.hist(data2, bins=25, alpha=0.7, label='Data 2')
plt.legend()
plt.show()
AThe histograms are plotted correctly but the legend is missing so it looks like one dataset.
BThe second histogram is not plotted because plt.hist overwrites the first call by default.
CThe second histogram is plotted but fully covers the first because both have the same color and no transparency.
DThe alpha value is too low, making the first histogram invisible.
Attempts:
2 left
💡 Hint
Check the default color and alpha values and how they affect visibility.
visualization
advanced
2:00remaining
Effect of different bin sizes on overlaid histograms
What happens visually when overlaying two histograms with different bin sizes?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(1)
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(1, 1, 1000)

plt.hist(data1, bins=15, alpha=0.5, label='Bins=15')
plt.hist(data2, bins=30, alpha=0.5, label='Bins=30')
plt.legend()
plt.show()
ABoth histograms have bars of the same width because matplotlib normalizes bin sizes automatically.
BThe histogram with 15 bins has wider bars, the one with 30 bins has narrower bars, both visible overlapping with transparency.
COnly the histogram with 30 bins is visible because it overwrites the other.
DThe plot shows an error because bins must be the same for overlaying histograms.
Attempts:
2 left
💡 Hint
Think about how bin count affects bar width and how matplotlib draws multiple histograms.
🚀 Application
expert
2:30remaining
Interpreting overlapping histograms for two groups
You have two groups of exam scores. Group A scores center around 70, Group B around 85. You overlay histograms with bins=20 and alpha=0.4. What can you infer from the plot?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
score_A = np.random.normal(70, 10, 500)
score_B = np.random.normal(85, 10, 500)

plt.hist(score_A, bins=20, alpha=0.4, label='Group A')
plt.hist(score_B, bins=20, alpha=0.4, label='Group B')
plt.legend()
plt.show()
ABoth groups have identical score distributions because histograms overlap perfectly.
BThe histograms cannot be compared because alpha=0.4 makes bars too transparent to see.
CGroup A scored higher than Group B because its histogram bars are taller.
DGroup B generally scored higher than Group A, but there is some overlap in score ranges visible in the histogram.
Attempts:
2 left
💡 Hint
Look at where the peaks of the histograms are and how much they overlap.