0
0
Matplotlibdata~20 mins

Normalized histograms in Matplotlib - Practice Problems & Coding Challenges

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