0
0
Matplotlibdata~20 mins

Bin count and bin edges in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bin Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of bin counts using numpy histogram
What is the output of the following code that calculates bin counts for the given data array?
Matplotlib
import numpy as np

data = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6])
bin_counts, bin_edges = np.histogram(data, bins=3)
print(bin_counts)
A[2 3 4]
B[3 3 3]
C[3 2 4]
D[4 3 2]
Attempts:
2 left
💡 Hint
Remember that numpy.histogram divides the data range into equal-width bins and counts how many values fall into each bin.
data_output
intermediate
2:00remaining
Bin edges from numpy histogram
What are the bin edges returned by numpy.histogram for the data below with 4 bins?
Matplotlib
import numpy as np

data = np.array([10, 15, 20, 25, 30, 35, 40])
bin_counts, bin_edges = np.histogram(data, bins=4)
print(bin_edges)
A[10. 17.5 25. 32.5 40.]
B[10. 20. 30. 40. 50.]
C[10. 15. 20. 25. 30.]
D[10. 18. 26. 34. 42.]
Attempts:
2 left
💡 Hint
Bin edges split the range from min to max into equal parts.
visualization
advanced
3:00remaining
Histogram bin count visualization
Which histogram plot corresponds to the bin counts produced by the code below?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.array([2, 4, 4, 4, 5, 5, 7, 9])
plt.hist(data, bins=3)
plt.show()
AHistogram with bins: [2-3), count=1; [3-6), count=5; [6-9], count=2
BHistogram with bins: [2-5), count=5; [5-7), count=2; [7-9], count=1
CHistogram with bins: [2-4), count=3; [4-7), count=4; [7-9], count=1
DHistogram with bins: [2-4), count=4; [4-6), count=2; [6-9], count=2
Attempts:
2 left
💡 Hint
Check how matplotlib divides the range into bins and counts data points in each bin.
🧠 Conceptual
advanced
1:30remaining
Effect of bin number on histogram shape
How does increasing the number of bins in a histogram affect the bin counts and bin edges?
AMore bins create narrower ranges, so bin counts generally decrease and bin edges increase in number.
BMore bins create wider ranges, so bin counts increase and bin edges decrease in number.
CMore bins do not affect bin counts or edges; they stay the same regardless of bin number.
DMore bins create fewer bins with more data points each, so bin counts increase and edges decrease.
Attempts:
2 left
💡 Hint
Think about splitting the data range into more parts.
🔧 Debug
expert
2:00remaining
Identify the error in bin edge calculation
What error will the following code produce and why?
Matplotlib
import numpy as np

data = np.array([1, 2, 3, 4, 5])
bin_counts, bin_edges = np.histogram(data, bins=[1, 2, 3])
print(bin_edges)
AIndexError: list index out of range
BNo error; outputs bin edges [1, 2, 3]
CValueError: bins must increase monotonically
DTypeError: unsupported operand type(s) for -: 'list' and 'int'
Attempts:
2 left
💡 Hint
Check if the bins argument is valid as a sequence of edges.