Challenge - 5 Problems
Histogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Histogram bin count output
What is the output of the following code snippet that creates a histogram and prints the counts of each bin?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6]) counts, bins, patches = plt.hist(data, bins=5) plt.close() print(counts)
Attempts:
2 left
💡 Hint
Remember that bins divide the data range into equal parts and counts show how many data points fall into each bin.
✗ Incorrect
The data ranges from 1 to 6. With 5 bins, the bins are [1,2), [2,3), [3,4), [4,5), [5,6]. The counts correspond to how many data points fall into each bin. For example, one 1 falls into the first bin and two 2s into the second (note: the last bin [5,6] is closed on the right to include the 6).
❓ data_output
intermediate1:30remaining
Number of bins in histogram
How many bins will the histogram have after running this code?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randint(0, 100, size=50) plt.hist(data, bins=10) plt.close() print(len(plt.hist(data, bins=10)[0]))
Attempts:
2 left
💡 Hint
The bins parameter controls how many bins the histogram has.
✗ Incorrect
When bins=10, the histogram divides the data range into exactly 10 bins, so the counts array length is 10.
❓ visualization
advanced2:30remaining
Effect of bin size on histogram shape
Which histogram plot best represents the data distribution when using 3 bins versus 15 bins for the same dataset?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.normal(loc=0, scale=1, size=1000) plt.subplot(1, 2, 1) plt.hist(data, bins=3) plt.title('3 bins') plt.subplot(1, 2, 2) plt.hist(data, bins=15) plt.title('15 bins') plt.tight_layout() plt.show()
Attempts:
2 left
💡 Hint
More bins mean more detail but can be noisy; fewer bins mean smoother but less detail.
✗ Incorrect
With 3 bins, the histogram groups data broadly, showing a smooth shape. With 15 bins, it reveals more detailed distribution features like peaks and valleys.
🧠 Conceptual
advanced1:30remaining
Understanding histogram bin edges
Given the code below, what is the length of the bins array returned by plt.hist when bins=7?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.arange(14) counts, bins, patches = plt.hist(data, bins=7) plt.close() print(len(bins))
Attempts:
2 left
💡 Hint
Bins array contains the edges of each bin, so it has one more element than the number of bins.
✗ Incorrect
The bins array contains the edges of the bins, so if there are 7 bins, there are 8 edges.
🔧 Debug
expert2:00remaining
Identify the error in histogram plotting code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.array([1, 2, 3, 4, 5]) plt.hist(data, bins='five') plt.show()
Attempts:
2 left
💡 Hint
The bins parameter expects an integer or a sequence of bin edges, not a string.
✗ Incorrect
Passing a string like 'five' to bins causes a ValueError because matplotlib expects an int or sequence for bins.