Bird
Raised Fist0
Matplotlibdata~20 mins

Cumulative histograms in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does setting cumulative=true in plt.hist() do to the histogram?
easy
A. It shows the total counts accumulated up to each bin.
B. It changes the histogram to a bar chart.
C. It normalizes the histogram to show probabilities.
D. It colors the bars based on their height.

Solution

  1. Step 1: Understand the cumulative parameter

    Setting cumulative=True in plt.hist() changes the histogram to show accumulated counts.
  2. Step 2: Effect on histogram output

    Instead of showing counts per bin, it shows the sum of counts up to that bin, making the histogram cumulative.
  3. Final Answer:

    It shows the total counts accumulated up to each bin. -> Option A
  4. Quick Check:

    cumulative=True means accumulation [OK]
Hint: Remember cumulative=True sums counts up to each bin [OK]
Common Mistakes:
  • Thinking it normalizes the histogram
  • Confusing cumulative with coloring
  • Assuming it changes chart type to bar chart
2. Which of the following is the correct syntax to create a cumulative histogram of data array data using matplotlib?
easy
A. plt.hist(data, cumulative=True)
B. plt.hist(data, cumulative=0)
C. plt.hist(data, cumulate=True)
D. plt.hist(data, cumulative)

Solution

  1. Step 1: Check correct parameter name and usage

    The correct parameter to make a histogram cumulative is cumulative=True.
  2. Step 2: Validate syntax correctness

    plt.hist(data, cumulative=True) uses the exact correct syntax. plt.hist(data, cumulative=0) uses 0 instead of True (no cumulative effect). plt.hist(data, cumulate=True) misspells the parameter. plt.hist(data, cumulative) is invalid syntax.
  3. Final Answer:

    plt.hist(data, cumulative=True) -> Option A
  4. Quick Check:

    Use cumulative=True exactly [OK]
Hint: Use cumulative=True exactly for cumulative histograms [OK]
Common Mistakes:
  • Misspelling 'cumulative' parameter
  • Using cumulative without assignment
  • Using cumulative=0 (disables cumulative)
3. What will be the output counts of the cumulative histogram bins for data = [1, 2, 2, 3, 4] with bins [1, 2, 3, 4, 5] using plt.hist(data, bins=[1,2,3,4,5], cumulative=True)?
medium
A. [0, 1, 3, 5]
B. [1, 3, 4, 5]
C. [1, 1, 2, 3]
D. [1, 2, 3, 4]

Solution

  1. Step 1: Count data points in each bin

    Bins: [1-2), [2-3), [3-4), [4-5). Counts per bin: 1 in first, 2 in second, 1 in third, 1 in fourth.
  2. Step 2: Calculate cumulative counts

    Cumulative sums: 1, 1+2=3, 3+1=4, 4+1=5.
  3. Final Answer:

    [1, 3, 4, 5] -> Option B
  4. Quick Check:

    Cumulative sums of counts = [1,3,4,5] [OK]
Hint: Add counts bin by bin for cumulative totals [OK]
Common Mistakes:
  • Counting bins inclusively instead of half-open
  • Not summing counts cumulatively
  • Miscounting data points per bin
4. The code below is intended to plot a cumulative histogram but does not produce the expected cumulative effect. What is the error?
import matplotlib.pyplot as plt
data = [1,2,2,3,4]
plt.hist(data, bins=4, cumulative=False)
plt.show()
medium
A. The bins parameter must be a list, not an integer.
B. plt.show() is missing and must be added.
C. The parameter cumulative should be set to True, not False.
D. Data list must be sorted before plotting.

Solution

  1. Step 1: Identify the cumulative parameter usage

    The code sets cumulative=False, so the histogram is not cumulative.
  2. Step 2: Correct the parameter to get cumulative histogram

    Changing cumulative=False to cumulative=True will produce the cumulative histogram as intended.
  3. Final Answer:

    The parameter cumulative should be set to True, not False. -> Option C
  4. Quick Check:

    cumulative=True needed for cumulative histogram [OK]
Hint: Set cumulative=True to enable cumulative histogram [OK]
Common Mistakes:
  • Leaving cumulative=False by mistake
  • Thinking bins must be a list always
  • Believing data must be sorted
  • Forgetting plt.show() (though present here)
5. You have two datasets: data1 = [1,2,3,4,5] and data2 = [2,3,4,5,6]. You want to plot their cumulative histograms on the same plot to compare their distributions. Which code snippet correctly does this?
hard
A. plt.hist([data1, data2], bins=6, cumulative=False, label=['Data1', 'Data2']); plt.legend(); plt.show()
B. plt.hist(data1 + data2, bins=6, cumulative=True); plt.show()
C. plt.hist(data1, bins=6); plt.hist(data2, bins=6); plt.show()
D. plt.hist([data1, data2], bins=6, cumulative=True, label=['Data1', 'Data2'], histtype='step'); plt.legend(); plt.show()

Solution

  1. Step 1: Understand plotting multiple cumulative histograms

    To compare cumulative histograms, pass both datasets as a list, set cumulative=True, and use histtype='step' for clarity.
  2. Step 2: Check options for correct parameters

    plt.hist([data1, data2], bins=6, cumulative=True, label=['Data1', 'Data2'], histtype='step'); plt.legend(); plt.show() correctly uses plt.hist([data1, data2], bins=6, cumulative=True, label=['Data1', 'Data2'], histtype='step') with legend and show. plt.hist(data1 + data2, bins=6, cumulative=True); plt.show() incorrectly combines the datasets into one histogram. plt.hist(data1, bins=6); plt.hist(data2, bins=6); plt.show() is not cumulative. plt.hist([data1, data2], bins=6, cumulative=False, label=['Data1', 'Data2']); plt.legend(); plt.show() is not cumulative.
  3. Final Answer:

    plt.hist([data1, data2], bins=6, cumulative=True, label=['Data1', 'Data2'], histtype='step'); plt.legend(); plt.show() -> Option D
  4. Quick Check:

    Multiple datasets + cumulative=True + histtype='step' = plt.hist([data1, data2], bins=6, cumulative=True, label=['Data1', 'Data2'], histtype='step'); plt.legend(); plt.show() [OK]
Hint: Use list of datasets and cumulative=True with histtype='step' [OK]
Common Mistakes:
  • Combining datasets into a single histogram
  • Forgetting cumulative=True
  • Not using histtype='step' for clear lines
  • Using cumulative=False by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard