0
0
Matplotlibdata~20 mins

Cumulative histograms in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cumulative Histogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of cumulative histogram with uniform data
What is the output array of counts for the cumulative histogram of 10 numbers from 1 to 10 with 5 bins?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

data = np.arange(1, 11)
counts, bins, patches = plt.hist(data, bins=5, cumulative=True)
plt.close()
print(counts)
A[2. 3. 5. 7. 10.]
B[1. 2. 3. 4. 5.]
C[0. 2. 4. 6. 8.]
D[2. 4. 6. 8. 10.]
Attempts:
2 left
💡 Hint
Think about how cumulative counts add up across bins.
data_output
intermediate
1:30remaining
Number of bins in cumulative histogram
If you create a cumulative histogram with 7 bins from data of 50 random values, how many count values will the output array have?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(50)
counts, bins, patches = plt.hist(data, bins=7, cumulative=True)
plt.close()
print(len(counts))
A50
B6
C7
D8
Attempts:
2 left
💡 Hint
The counts array length matches the number of bins.
visualization
advanced
2:30remaining
Identify the cumulative histogram plot
Which plot shows a cumulative histogram of data sampled from a normal distribution?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(0, 1, 1000)
plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.hist(data, bins=30)
plt.title('Regular Histogram')
plt.subplot(1,2,2)
plt.hist(data, bins=30, cumulative=True)
plt.title('Cumulative Histogram')
plt.tight_layout()
plt.show()
AThe right plot with bars increasing and never decreasing
BNeither plot is a histogram
CBoth plots show cumulative histograms
DThe left plot with bars showing frequency counts
Attempts:
2 left
💡 Hint
Cumulative histograms always increase or stay the same as bins increase.
🧠 Conceptual
advanced
1:30remaining
Effect of cumulative=True on histogram counts
What happens to the counts array when you set cumulative=True in plt.hist compared to cumulative=False?
ACounts become negative for bins with no data
BCounts show total data points up to each bin edge, increasing or staying the same
CCounts show only the data points in each bin, independent of previous bins
DCounts are normalized to sum to 1
Attempts:
2 left
💡 Hint
Cumulative means adding up counts as you move through bins.
🔧 Debug
expert
1:30remaining
Identify the error in cumulative histogram code
What error does this code raise? import matplotlib.pyplot as plt plt.hist([1,2,3,4,5], bins=3, cumulative='yes')
Matplotlib
import matplotlib.pyplot as plt

plt.hist([1,2,3,4,5], bins=3, cumulative='yes')
ATypeError: cumulative must be a boolean, not str
BValueError: bins must be an integer or sequence
CSyntaxError: invalid syntax
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the type expected for the cumulative parameter.