Horizontal bar chart with plt.barh in Matplotlib - Time & Space Complexity
We want to understand how the time to draw a horizontal bar chart changes as we add more bars.
How does the work grow when the number of bars increases?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D', 'E']
values = [5, 7, 3, 8, 4]
plt.barh(labels, values)
plt.show()
This code draws a horizontal bar chart with 5 bars labeled A to E with given values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing each bar on the chart.
- How many times: Once for each label and value pair (here 5 times).
As the number of bars increases, the drawing work grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 drawing steps |
| 100 | 100 drawing steps |
| 1000 | 1000 drawing steps |
Pattern observation: Doubling the bars doubles the drawing work.
Time Complexity: O(n)
This means the time to draw the chart grows directly with the number of bars.
[X] Wrong: "Drawing a bar chart takes the same time no matter how many bars there are."
[OK] Correct: Each bar needs to be drawn separately, so more bars mean more work.
Understanding how drawing time grows helps you explain performance when visualizing data with many points.
What if we added nested loops to draw grouped bars? How would the time complexity change?
Practice
plt.barh() function in matplotlib do?Solution
Step 1: Understand the function purpose
plt.barh()is designed to create horizontal bar charts, where bars extend from left to right.Step 2: Compare with other plot types
Vertical bar charts useplt.bar(), line graphs useplt.plot(), and scatter plots useplt.scatter().Final Answer:
Creates a horizontal bar chart with bars extending left to right -> Option CQuick Check:
Horizontal bar chart = plt.barh() [OK]
- Confusing plt.barh() with plt.bar()
- Thinking plt.barh() creates vertical bars
- Mixing bar charts with line or scatter plots
Solution
Step 1: Identify correct function and argument order
plt.barh()takes categories as the first argument (y-axis labels) and values as the second argument (bar lengths).Step 2: Check each option
plt.barh(['A', 'B', 'C'], [5, 7, 3]) correctly usesplt.barh(['A', 'B', 'C'], [5, 7, 3]). plt.bar([5, 7, 3], ['A', 'B', 'C']) uses vertical bars and reverses arguments. plt.barh([5, 7, 3], ['A', 'B', 'C']) reverses arguments for horizontal bars. plt.plot(['A', 'B', 'C'], [5, 7, 3]) plots a line graph.Final Answer:
plt.barh(['A', 'B', 'C'], [5, 7, 3]) -> Option AQuick Check:
Categories first, values second in plt.barh() [OK]
- Swapping categories and values in arguments
- Using plt.bar() instead of plt.barh() for horizontal bars
- Using plt.plot() instead of bar chart functions
import matplotlib.pyplot as plt categories = ['X', 'Y', 'Z'] values = [10, 5, 8] plt.barh(categories, values, color='green') plt.show()
Solution
Step 1: Analyze the function and parameters
The code usesplt.barh()with categories on y-axis and values as bar lengths, colored green.Step 2: Understand the plot output
This produces a horizontal bar chart with green bars labeled X, Y, Z with lengths 10, 5, and 8 respectively.Final Answer:
A horizontal bar chart with green bars for categories X, Y, Z -> Option DQuick Check:
plt.barh() + color='green' = horizontal green bars [OK]
- Thinking plt.barh() creates vertical bars
- Assuming color must be a list
- Confusing bar chart with line plot
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] values = [4, 6, 8] plt.barh(values, labels) plt.show()
Solution
Step 1: Check argument order in plt.barh()
The first argument should be categories (labels), the second argument should be values (bar lengths).Step 2: Identify the mistake in the code
The code passes values first and labels second, which reverses the expected order and causes incorrect plotting.Final Answer:
Arguments are reversed; categories should be first, values second -> Option BQuick Check:
plt.barh(categories, values) order matters [OK]
- Swapping the order of categories and values
- Forgetting plt.show() (though present here)
- Thinking color argument is mandatory
Solution
Step 1: Use plt.barh() with correct arguments
Categories come first, then values. Set color='red' and height=0.5 to adjust bar thickness.Step 2: Add sales values as text labels on bars
Use a for loop withplt.text()to place sales numbers slightly right of each bar end.Step 3: Verify other options
plt.barh([200, 150, 300], ['Jan', 'Feb', 'Mar'], color='red', height=0.5) plt.show() reverses arguments, plt.bar(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', height=0.5) plt.show() uses vertical bars, plt.barh(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', width=0.5) plt.show() uses 'width' instead of 'height' which is invalid for barh.Final Answer:
plt.barh(['Jan', 'Feb', 'Mar'], [200, 150, 300], color='red', height=0.5) for i, v in enumerate([200, 150, 300]): plt.text(v + 5, i, str(v)) plt.show() -> Option AQuick Check:
plt.barh(categories, values, height=0.5) + plt.text() labels [OK]
- Swapping categories and values
- Using width instead of height for bar thickness
- Not adding text labels or placing them incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
