Bird
Raised Fist0
Matplotlibdata~20 mins

Normalized histograms in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Normalized Histogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of normalized histogram with density=True
What is the output of the following code snippet that plots a normalized histogram using matplotlib?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
data = np.random.normal(loc=0, scale=1, size=1000)

counts, bins, patches = plt.hist(data, bins=10, density=True)
plt.close()
print(counts.round(3))
A[0.04 0.07 0.12 0.18 0.22 0.19 0.12 0.05 0.01 0. ]
B[0.04 0.07 0.12 0.18 0.22 0.19 0.12 0.05 0.01 0.01]
C[40. 70. 120. 180. 220. 190. 120. 50. 10. 0.]
D[0.004 0.007 0.012 0.018 0.022 0.019 0.012 0.005 0.001 0. ]
Attempts:
2 left
💡 Hint
Remember that density=True scales counts so the area sums to 1, not the counts themselves.
❓ data_output
intermediate
1:30remaining
Number of bins with non-zero density in normalized histogram
Given the following code that creates a normalized histogram, how many bins have a non-zero density value?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)
data = np.random.uniform(0, 5, 500)
counts, bins, _ = plt.hist(data, bins=5, density=True)
plt.close()
print(sum(counts > 0))
A4
B5
C3
D0
Attempts:
2 left
💡 Hint
Uniform data over the range 0 to 5 with 5 bins should fill all bins.
❓ visualization
advanced
2:30remaining
Effect of density parameter on histogram area
Which histogram plot correctly shows a normalized histogram where the total area under the bars sums to 1?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(2)
data = np.random.exponential(scale=1.0, size=1000)

plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.hist(data, bins=20, density=False)
plt.title('Histogram with density=False')

plt.subplot(1,2,2)
plt.hist(data, bins=20, density=True)
plt.title('Histogram with density=True')
plt.close()
ABoth plots area sum to 1
BBoth plots area sum to 1000
CLeft plot area sums to 1, right plot area sums to 1000
DLeft plot area sums to 1000, right plot area sums to 1
Attempts:
2 left
💡 Hint
density=True normalizes the histogram so the area sums to 1.
🔧 Debug
advanced
1:30remaining
Identify the error in normalized histogram code
What error will this code raise when trying to plot a normalized histogram?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(100)
plt.hist(data, bins=10, density='True')
plt.show()
ATypeError: 'str' object cannot be interpreted as a boolean
BValueError: bins must be an integer or sequence
CNo error, plot shows correctly
DNameError: name 'True' is not defined
Attempts:
2 left
💡 Hint
Check the type of the density parameter value.
🚀 Application
expert
2:30remaining
Calculate probability from normalized histogram bins
Given a normalized histogram with bin edges [0, 1, 2, 3] and densities [0.2, 0.3, 0.5], what is the probability that a random value falls between 1 and 3?
A0.3
B0.5
C0.8
D1.0
Attempts:
2 left
💡 Hint
Probability for a bin = density * bin width. Sum probabilities for bins between 1 and 3.

Practice

(1/5)
1. What does setting density=True in plt.hist() do to the histogram?
easy
A. It changes the histogram bars to a different color.
B. It sorts the data before plotting the histogram.
C. It normalizes the histogram so the area sums to 1, showing probability density.
D. It plots the histogram with logarithmic scale on the y-axis.

Solution

  1. Step 1: Understand the role of density=True

    Setting density=True scales the histogram so the total area under the bars equals 1.
  2. Step 2: Interpret the effect on histogram

    This scaling means the histogram shows probability density instead of raw counts, useful for comparing distributions.
  3. Final Answer:

    It normalizes the histogram so the area sums to 1, showing probability density. -> Option C
  4. Quick Check:

    Normalized histogram = density=True [OK]
Hint: Remember: density=True makes area = 1, not counts [OK]
Common Mistakes:
  • Thinking density=True changes colors
  • Assuming density=True sorts data
  • Confusing density=True with log scale
2. Which of the following is the correct syntax to plot a normalized histogram of data array data using matplotlib?
easy
A. plt.hist(data, scale='density')
B. plt.hist(data, density=True)
C. plt.hist(data, norm=True)
D. plt.hist(data, normalize=True)

Solution

  1. Step 1: Recall matplotlib histogram parameters

    The correct parameter to normalize histogram counts is density=True.
  2. Step 2: Check each option

    The other options use incorrect or nonexistent parameters for normalization.
  3. Final Answer:

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

    Use density=True for normalized histogram [OK]
Hint: Use density=True exactly, no shortcuts [OK]
Common Mistakes:
  • Using normalize=True which is invalid
  • Trying norm=True instead of density=True
  • Passing scale='density' which is not a parameter
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
data = np.random.randint(1, 5, size=100)
counts, bins, patches = plt.hist(data, bins=4, density=True)
print(counts.round(2))
medium
A. [0.27 0.25 0.23 0.25]
B. [0.25 0.25 0.25 0.25]
C. [25 25 25 25]
D. [0.20 0.30 0.25 0.25]

Solution

  1. Step 1: Understand data and bins

    Data has 100 integers between 1 and 4, bins=4 means bins cover 1-2, 2-3, 3-4, 4-5.
  2. Step 2: Calculate normalized counts

    Using density=True, counts represent probability density, roughly proportional to frequency divided by bin width. The printed counts are close to [0.27, 0.25, 0.23, 0.25].
  3. Final Answer:

    [0.27 0.25 0.23 0.25] -> Option A
  4. Quick Check:

    Normalized counts sum to ~1 area [OK]
Hint: density=True scales counts to sum area 1 [OK]
Common Mistakes:
  • Expecting raw counts instead of densities
  • Confusing counts with percentages
  • Ignoring bin width effect on density
4. Identify the error in this code that tries to plot a normalized histogram:
import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 3, 3]
plt.hist(data, norm=True)
plt.show()
medium
A. plt.show() is missing and needed to display the plot.
B. The data list must be converted to a numpy array first.
C. Bins parameter is missing, causing an error.
D. The parameter 'norm' is invalid; should be 'density'.

Solution

  1. Step 1: Check parameter names in plt.hist()

    The correct parameter to normalize histogram is density=True, not norm=True.
  2. Step 2: Verify other parts

    Data can be a list, plt.show() is present, and bins are optional. So no error from those.
  3. Final Answer:

    The parameter 'norm' is invalid; should be 'density'. -> Option D
  4. Quick Check:

    Use density=True, not norm=True [OK]
Hint: Use density=True, not norm=True [OK]
Common Mistakes:
  • Using norm=True instead of density=True
  • Thinking data must be numpy array
  • Forgetting plt.show() (though present here)
  • Assuming bins are mandatory
5. You have two datasets with different sizes: data1 has 1000 points, data2 has 200 points. You want to compare their distributions fairly using histograms. Which approach is best?
hard
A. Plot histograms with density=True for both datasets.
B. Plot histograms with default counts for both datasets.
C. Plot histograms with different bin sizes for each dataset.
D. Plot histograms using cumulative counts.

Solution

  1. Step 1: Understand the problem of different sizes

    Raw counts histograms will show higher bars for the larger dataset, making comparison unfair.
  2. Step 2: Use normalized histograms

    Setting density=True scales histograms to probability densities, allowing fair comparison regardless of sample size.
  3. Final Answer:

    Plot histograms with density=True for both datasets. -> Option A
  4. Quick Check:

    Normalize histograms to compare different sizes [OK]
Hint: Normalize histograms to compare different sample sizes [OK]
Common Mistakes:
  • Comparing raw counts directly
  • Using different bin sizes causing bias
  • Using cumulative counts which show totals, not densities
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