📖 Scenario: You are analyzing the daily sales numbers of a small store over a week. You want to understand how sales accumulate over the days.
🎯 Goal: Create a cumulative histogram using matplotlib to visualize the accumulation of sales over the week.
📋 What You'll Learn
Create a list called daily_sales with exact sales numbers for 7 days
Create a variable called bins to define histogram bins
Use plt.hist() with cumulative=True to create a cumulative histogram
Display the histogram with plt.show()
💡 Why This Matters
🌍 Real World
Cumulative histograms help businesses see how quantities add up over time or categories, like total sales or accumulated customers.
💼 Career
Data analysts and scientists use cumulative histograms to understand trends and distributions in data for better decision-making.
Progress0 / 4 steps
1
Create the daily sales data
Create a list called daily_sales with these exact values: 5, 8, 6, 10, 7, 9, 4 representing sales for 7 days.
Matplotlib
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Define histogram bins
Create a variable called bins and set it to a list with these exact values: 0, 3, 6, 9, 12 to define the histogram bins.
Matplotlib
Hint
Bins are the edges for grouping data in the histogram. Use a list with the exact numbers.
3
Create the cumulative histogram
Import matplotlib.pyplot as plt. Then use plt.hist() with daily_sales, bins, and cumulative=True to create a cumulative histogram.
Matplotlib
Hint
Use import matplotlib.pyplot as plt to import. Then call plt.hist() with the right arguments.
4
Display the histogram
Use plt.show() to display the cumulative histogram plot.
Matplotlib
Hint
Call plt.show() to open the plot window and see the histogram.
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
Step 1: Understand the cumulative parameter
Setting cumulative=True in plt.hist() changes the histogram to show accumulated counts.
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.
Final Answer:
It shows the total counts accumulated up to each bin. -> Option A
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
Step 1: Check correct parameter name and usage
The correct parameter to make a histogram cumulative is cumulative=True.
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.
Final Answer:
plt.hist(data, cumulative=True) -> Option A
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
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.
Step 2: Calculate cumulative counts
Cumulative sums: 1, 1+2=3, 3+1=4, 4+1=5.
Final Answer:
[1, 3, 4, 5] -> Option B
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
Step 1: Identify the cumulative parameter usage
The code sets cumulative=False, so the histogram is not cumulative.
Step 2: Correct the parameter to get cumulative histogram
Changing cumulative=False to cumulative=True will produce the cumulative histogram as intended.
Final Answer:
The parameter cumulative should be set to True, not False. -> Option C
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()