Bird
Raised Fist0
Matplotlibdata~10 mins

Multiple histograms overlay in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the matplotlib plotting library.

Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Amatplotlib
Bmatplotlib.pyplot
Cpyplot
Dplt
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'matplotlib' directly instead of 'matplotlib.pyplot'.
Using 'import pyplot as plt' without 'matplotlib'.
2fill in blank
medium

Complete the code to create two lists of data for histograms.

Matplotlib
data1 = [1, 2, 3, 4, 5]
data2 = [1]
Drag options to blanks, or click blank then click option'
A[2, 3, 4, 5, 6]
B[5, 4, 3, 2, 1]
C[1, 1, 2, 2, 3]
D[6, 7, 8, 9, 10]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same list for both data sets.
Using an empty list.
3fill in blank
hard

Fix the error in the code to plot the first histogram with 5 bins.

Matplotlib
plt.hist(data1, bins=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'5'
B[5]
Cbins=5
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing bins as a string like '5'.
Passing bins as a list like [5].
4fill in blank
hard

Fill both blanks to plot two histograms overlayed with transparency.

Matplotlib
plt.hist(data1, bins=[1], alpha=[2])
plt.hist(data2, bins=5, alpha=0.5)
plt.show()
Drag options to blanks, or click blank then click option'
A5
B0.5
C1
D0.8
Attempts:
3 left
💡 Hint
Common Mistakes
Using different bins for the two histograms.
Using alpha=1 which hides the histogram underneath.
5fill in blank
hard

Fill all three blanks to add labels and a legend to the overlayed histograms.

Matplotlib
plt.hist(data1, bins=5, alpha=0.5, label=[1])
plt.hist(data2, bins=5, alpha=0.5, label=[2])
plt.[3]()
plt.show()
Drag options to blanks, or click blank then click option'
A"Data 1"
B"Data 2"
Clegend
Dshow_legend
Attempts:
3 left
💡 Hint
Common Mistakes
Not adding labels to histograms.
Using plt.show() instead of plt.legend() to show legend.

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

  1. Step 1: Understand histogram purpose

    A histogram shows how data values are distributed across ranges.
  2. Step 2: Purpose of overlaying

    Overlaying multiple histograms lets us see and compare distributions of different groups in one plot.
  3. Final Answer:

    To visually compare the distribution of different data groups in one plot -> Option D
  4. 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

  1. Step 1: Check correct parameter for transparency

    In matplotlib, alpha controls transparency and must be between 0 and 1.
  2. 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.
  3. Final Answer:

    plt.hist(data1, alpha=0.5); plt.hist(data2, alpha=0.5) -> Option B
  4. Quick Check:

    Use alpha between 0 and 1 for transparency [OK]
Hint: Use alpha between 0 and 1 for transparency [OK]
Common Mistakes:
  • Using alpha values greater than 1
  • Using wrong parameter names like transparency
  • Omitting alpha causing no overlay visibility
3. What will be the output of this code?
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
data1 = np.random.normal(0, 1, 100)
data2 = np.random.normal(2, 1, 100)
plt.hist(data1, bins=20, alpha=0.6, label='Group 1')
plt.hist(data2, bins=20, alpha=0.6, label='Group 2')
plt.legend()
plt.show()
medium
A. Two overlapping histograms with semi-transparent bars and a legend showing 'Group 1' and 'Group 2'
B. A single histogram combining data1 and data2 without transparency
C. An error because bins parameter is missing
D. Two histograms plotted side by side without overlap

Solution

  1. Step 1: Analyze histogram parameters

    Both histograms use 20 bins and alpha=0.6 for transparency, so bars will overlap but remain visible.
  2. Step 2: Check legend and plot behavior

    Labels are set and legend is called, so legend will show 'Group 1' and 'Group 2'. Histograms overlay, not side by side.
  3. Final Answer:

    Two overlapping histograms with semi-transparent bars and a legend showing 'Group 1' and 'Group 2' -> Option A
  4. Quick Check:

    Alpha + legend = overlapping histograms with labels [OK]
Hint: Alpha makes bars transparent; legend shows labels [OK]
Common Mistakes:
  • Thinking histograms combine data automatically
  • Expecting side-by-side bars without specifying
  • Missing legend despite label parameter
4. Identify the error in this code that tries to overlay two histograms:
plt.hist(data1, bins=15, alpha=0.4, label='A')
plt.hist(data2, bins=15, alpha=0.4)
plt.legend()
plt.show()
medium
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

  1. 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.
  2. Step 2: Validate other parameters

    Alpha=0.4 is valid. Bins can be same for overlay. Legend can be called after plotting.
  3. Final Answer:

    Missing label for the second histogram causes legend to show only one label -> Option C
  4. 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

  1. Step 1: Understand size difference impact

    Datasets with different sizes produce histograms with very different counts, making small datasets hard to see.
  2. 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.
  3. Final Answer:

    Use density=True in plt.hist for all datasets and set alpha=0.5 with labels -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard