📖 Scenario: You are analyzing the sales data of two different stores. You want to compare the distribution of daily sales amounts for both stores visually.
🎯 Goal: Create two lists of daily sales amounts for Store A and Store B. Then, overlay their histograms on the same plot to compare their sales distributions.
📋 What You'll Learn
Create two lists named store_a_sales and store_b_sales with given sales data.
Create a variable bins to set the number of bins for the histograms.
Use matplotlib.pyplot.hist to plot both histograms on the same figure with transparency.
Add a legend to distinguish Store A and Store B histograms.
Display the plot using plt.show().
💡 Why This Matters
🌍 Real World
Overlaying histograms helps compare distributions of data from different groups, like sales from different stores or test scores from different classes.
💼 Career
Data scientists often compare multiple datasets visually to find patterns or differences, making histogram overlays a useful skill.
Progress0 / 4 steps
1
Create sales data lists
Create two lists called store_a_sales and store_b_sales with these exact values: store_a_sales = [20, 35, 30, 35, 27, 25, 32] and store_b_sales = [25, 32, 34, 20, 25, 40, 38].
Matplotlib
Hint
Use square brackets to create lists with the exact numbers given.
2
Set number of bins for histograms
Create a variable called bins and set it to 5 to define the number of bins for the histograms.
Matplotlib
Hint
Just assign the number 5 to a variable named bins.
3
Plot the histograms overlay
Import matplotlib.pyplot as plt. Use plt.hist to plot store_a_sales and store_b_sales on the same figure with bins bins, set alpha=0.5 for transparency, and add labels 'Store A' and 'Store B' respectively. Then add a legend with plt.legend().
Matplotlib
Hint
Use plt.hist() twice with the correct parameters and then call plt.legend().
4
Show the plot
Add a line to display the plot using plt.show().
Matplotlib
Hint
Use plt.show() to display the histogram plot window.
Practice
(1/5)
1. What is the main purpose of overlaying multiple histograms in a single plot using matplotlib?
easy
A. To sort data values in ascending order
B. To create a pie chart from histogram data
C. To calculate the mean of multiple datasets
D. To visually compare the distribution of different data groups in one plot
Solution
Step 1: Understand histogram purpose
A histogram shows how data values are distributed across ranges.
Step 2: Purpose of overlaying
Overlaying multiple histograms lets us see and compare distributions of different groups in one plot.
Final Answer:
To visually compare the distribution of different data groups in one plot -> Option D
Quick Check:
Overlay = Compare distributions [OK]
Hint: Overlay histograms to compare groups visually [OK]
Common Mistakes:
Thinking overlay sorts data
Confusing histogram with pie chart
Assuming overlay calculates statistics
2. Which of the following matplotlib code snippets correctly overlays two histograms with transparency?
easy
A. plt.hist(data1); plt.hist(data2, alpha=2)
B. plt.hist(data1, alpha=0.5); plt.hist(data2, alpha=0.5)
C. plt.hist(data1, transparency=0.5); plt.hist(data2, transparency=0.5)
D. plt.hist(data1, alpha=1.5); plt.hist(data2, alpha=1.5)
Solution
Step 1: Check correct parameter for transparency
In matplotlib, alpha controls transparency and must be between 0 and 1.
Step 2: Validate each option
plt.hist(data1, alpha=0.5); plt.hist(data2, alpha=0.5) uses alpha=0.5 correctly for both histograms. Options B and D use invalid alpha values (>1). plt.hist(data1, transparency=0.5); plt.hist(data2, transparency=0.5) uses a wrong parameter name transparency.
Final Answer:
plt.hist(data1, alpha=0.5); plt.hist(data2, alpha=0.5) -> Option B
Quick Check:
Use alpha between 0 and 1 for transparency [OK]
Hint: Use alpha between 0 and 1 for transparency [OK]
A. Alpha value 0.4 is invalid and causes runtime error
B. Bins parameter must be different for each histogram
C. Missing label for the second histogram causes legend to show only one label
D. plt.legend() must be called before plotting histograms
Solution
Step 1: Check labels for legend
The first histogram has label 'A', but the second histogram has no label, so legend will show only one entry.
Step 2: Validate other parameters
Alpha=0.4 is valid. Bins can be same for overlay. Legend can be called after plotting.
Final Answer:
Missing label for the second histogram causes legend to show only one label -> Option C
Quick Check:
All histograms need labels for legend entries [OK]
Hint: Add labels to all histograms for complete legend [OK]
Common Mistakes:
Assuming alpha=0.4 is invalid
Thinking bins must differ for overlay
Calling legend before plotting causes error
5. You have three datasets with very different sizes: data1 (1000 points), data2 (100 points), and data3 (10 points). You want to overlay their histograms fairly so that the smaller datasets are visible and comparable. Which approach is best?
hard
A. Use density=True in plt.hist for all datasets and set alpha=0.5 with labels
B. Plot histograms without transparency and use default counts
C. Use different bin sizes for each dataset without normalization
D. Plot only the largest dataset to avoid clutter
Solution
Step 1: Understand size difference impact
Datasets with different sizes produce histograms with very different counts, making small datasets hard to see.
Step 2: Use density normalization and transparency
Setting density=True normalizes histograms to show probability density, making them comparable regardless of size. Using alpha=0.5 allows overlapping visibility. Adding labels helps identify each.
Final Answer:
Use density=True in plt.hist for all datasets and set alpha=0.5 with labels -> Option A
Quick Check:
Normalize with density=True + alpha for visibility [OK]
Hint: Normalize histograms with density=True for fair comparison [OK]
Common Mistakes:
Not normalizing causes small datasets to be invisible
Using opaque bars hides overlaps
Changing bins without normalization confuses comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.