Challenge - 5 Problems
Histogram Overlay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Look at the alpha parameter controlling transparency and how plt.hist overlays multiple calls.
✗ Incorrect
The code plots two histograms on the same axes with alpha=0.5, so bars overlap with transparency. The first dataset centers near 0, the second near 2. The legend shows labels for both.
❓ data_output
intermediate1: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()
Attempts:
2 left
💡 Hint
Each plt.hist call creates its own set of bars even if bins are the same.
✗ Incorrect
Each histogram call creates 20 bars. Overlaying two histograms with bins=20 results in 40 bars total, 20 per dataset, drawn on top of each other.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check the default color and alpha values and how they affect visibility.
✗ Incorrect
Both histograms use the default color (blue) and alpha=0.7, so the second fully covers the first. Transparency is not enough to see both distinctly.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
Think about how bin count affects bar width and how matplotlib draws multiple histograms.
✗ Incorrect
Different bin counts produce bars of different widths. Overlaying them shows wider bars for 15 bins and narrower bars for 30 bins, both visible with alpha transparency.
🚀 Application
expert2: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()
Attempts:
2 left
💡 Hint
Look at where the peaks of the histograms are and how much they overlap.
✗ Incorrect
Group B's histogram centers around 85, higher than Group A's 70. The overlap shows some scores are similar, but overall Group B scored higher.