0
0
Matplotlibdata~20 mins

Basic histogram with plt.hist in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Histogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Histogram bin count output
What is the output of the following code snippet that creates a histogram and prints the counts of each bin?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6])
counts, bins, patches = plt.hist(data, bins=5)
plt.close()
print(counts)
A[1. 2. 1. 1. 4.]
B[1. 2. 3. 2. 1.]
C[2. 1. 3. 2. 1.]
D[1. 3. 2. 2. 1.]
Attempts:
2 left
💡 Hint
Remember that bins divide the data range into equal parts and counts show how many data points fall into each bin.
data_output
intermediate
1:30remaining
Number of bins in histogram
How many bins will the histogram have after running this code?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(0, 100, size=50)
plt.hist(data, bins=10)
plt.close()
print(len(plt.hist(data, bins=10)[0]))
A9
B50
C10
D11
Attempts:
2 left
💡 Hint
The bins parameter controls how many bins the histogram has.
visualization
advanced
2:30remaining
Effect of bin size on histogram shape
Which histogram plot best represents the data distribution when using 3 bins versus 15 bins for the same dataset?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

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

plt.subplot(1, 2, 1)
plt.hist(data, bins=3)
plt.title('3 bins')

plt.subplot(1, 2, 2)
plt.hist(data, bins=15)
plt.title('15 bins')

plt.tight_layout()
plt.show()
A3 bins histogram shows many peaks; 15 bins histogram is very smooth and flat.
B3 bins histogram is very smooth and general; 15 bins histogram shows more detail and peaks.
CBoth histograms look identical regardless of bin count.
D3 bins histogram has more bars than 15 bins histogram.
Attempts:
2 left
💡 Hint
More bins mean more detail but can be noisy; fewer bins mean smoother but less detail.
🧠 Conceptual
advanced
1:30remaining
Understanding histogram bin edges
Given the code below, what is the length of the bins array returned by plt.hist when bins=7?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.arange(14)
counts, bins, patches = plt.hist(data, bins=7)
plt.close()
print(len(bins))
A8
B7
C6
D14
Attempts:
2 left
💡 Hint
Bins array contains the edges of each bin, so it has one more element than the number of bins.
🔧 Debug
expert
2:00remaining
Identify the error in histogram plotting code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.array([1, 2, 3, 4, 5])
plt.hist(data, bins='five')
plt.show()
ATypeError: 'str' object cannot be interpreted as an integer
BSyntaxError: invalid syntax
CNo error, histogram plots successfully
DValueError: bins must be a positive integer or sequence
Attempts:
2 left
💡 Hint
The bins parameter expects an integer or a sequence of bin edges, not a string.