Basic histogram with plt.hist in Matplotlib - Time & Space Complexity
We want to understand how the time it takes to create a histogram changes as we add more data points.
How does the work grow when the input data gets bigger?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
plt.hist(data, bins=30)
plt.show()
This code creates a histogram of 1000 random numbers divided into 30 bins.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Counting how many data points fall into each bin.
- How many times: Each of the n data points is checked once to find its bin.
As the number of data points increases, the time to count them into bins grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: Doubling the data roughly doubles the work.
Time Complexity: O(n)
This means the time to build the histogram grows linearly with the number of data points.
[X] Wrong: "The number of bins affects the time complexity a lot."
[OK] Correct: The bins are usually fixed and small compared to data size, so the main work depends on how many data points we have, not the bins.
Understanding how data size affects plotting helps you explain performance clearly and shows you can think about efficiency in real tasks.
"What if we increased the number of bins to be the same as the number of data points? How would the time complexity change?"
Practice
plt.hist() function do in matplotlib?Solution
Step 1: Understand the purpose of plt.hist()
The functionplt.hist()groups data into bins and shows how often values fall into each bin.Step 2: Compare with other plot types
Line graphs connect points, scatter plots show points, and pie charts show parts of a whole, which are different from histograms.Final Answer:
It creates a histogram to show data frequency distribution. -> Option CQuick Check:
Histogram = frequency bars [OK]
- Confusing histogram with line plot
- Thinking plt.hist() creates scatter plots
- Mixing histogram with pie chart
data using matplotlib?Solution
Step 1: Recall the correct function name
The correct function to create a histogram isplt.hist(), notplt.plot.hist()orplt.histogram().Step 2: Confirm usage with plt.show()
After plotting,plt.show()displays the figure. Usingplt.bar()creates bar charts, not histograms.Final Answer:
plt.hist(data); plt.show() -> Option DQuick Check:
Use plt.hist() for histograms [OK]
- Using plt.plot.hist() which is invalid
- Calling plt.histogram() which doesn't exist
- Using plt.bar() instead of plt.hist()
import matplotlib.pyplot as plt values = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] plt.hist(values, bins=4) plt.show()
Solution
Step 1: Understand bins parameter
Settingbins=4divides the data range into 4 intervals (bins).Step 2: Count frequencies in each bin
The values 1, 2, 3, and 4 fall into these bins with frequencies 1, 2, 3, and 4 respectively, so the histogram shows 4 bars with these heights.Final Answer:
A histogram with 4 bars showing frequencies: 1, 2, 3, and 4 -> Option AQuick Check:
Bins split data; bars show counts [OK]
- Thinking bins=4 means 10 bars
- Confusing histogram with line plot
- Assuming bins must be greater than data length
import matplotlib.pyplot as plt values = [5, 7, 8, 5, 6] plt.hist(values, bins='five') plt.show()
Solution
Step 1: Check bins parameter type
Thebinsargument must be an integer or sequence of numbers, not a string like 'five'.Step 2: Verify other parts
The data list is valid, color is optional, andplt.show()is present, so no other errors.Final Answer:
The bins parameter should be an integer, not a string. -> Option BQuick Check:
Bins must be int or list, not string [OK]
- Passing bins as a string instead of integer
- Thinking color is required
- Forgetting plt.show() (not the case here)
Solution
Step 1: Use bins=integer for equal-width
bins=5automatically divides the range (0-100) into 5 equal-width bins of 20 units each: [0-20, 20-40, 40-60, 60-80, 80-100].Step 2: Color the bars blue
Thecolor='blue'parameter sets the bar color correctly.Step 3: Eliminate distractors
B specifies explicit bins [0,20,50,70,90,100] with unequal widths (20,30,20,20,10); C uses invalid 'colors'; D has unquoted 'blue' causing NameError.Final Answer:
plt.hist(scores, bins=5, color='blue'); plt.show() -> Option AQuick Check:
bins=5 (equal width) + color='blue' [OK]
- Using explicit bins with unequal widths
- Using 'colors' instead of 'color'
- Using color=blue without quotes
Ready to prove it?
Pass the test and this pattern moves unknown
