Stacked bar charts in Matplotlib - Time & Space Complexity
When creating stacked bar charts, we want to know how the time to draw the chart changes as we add more data.
How does the drawing time grow when we increase the number of bars or stacks?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
values1 = [3, 5, 1, 7]
values2 = [2, 3, 4, 1]
plt.bar(labels, values1, label='Group 1')
plt.bar(labels, values2, bottom=values1, label='Group 2')
plt.legend()
plt.show()
This code draws a stacked bar chart with two groups stacked on four bars.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Drawing bars for each group and label.
- How many times: For each group, it loops over all labels once.
As we add more bars or more groups, the number of drawing steps grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 bars, 2 groups | 20 drawing steps |
| 100 bars, 2 groups | 200 drawing steps |
| 1000 bars, 5 groups | 5000 drawing steps |
Pattern observation: The total drawing steps grow by multiplying the number of bars by the number of groups.
Time Complexity: O(n * g)
This means the time to draw grows linearly with the number of bars (n) and the number of stacked groups (g).
[X] Wrong: "Adding more groups does not affect drawing time much because bars are stacked."
[OK] Correct: Each group adds a full set of bars to draw, so more groups multiply the work.
Understanding how drawing time grows helps you explain performance when visualizing large datasets, a useful skill in data science roles.
What if we used a grouped bar chart instead of stacked bars? How would the time complexity change?
Practice
stacked bar chart in data visualization?Solution
Step 1: Understand the concept of stacked bar charts
Stacked bar charts combine multiple data parts into one bar to show totals and parts.Step 2: Identify the purpose of stacking
Stacking helps visualize how each part contributes to the total for each group.Final Answer:
To show how different parts contribute to the total within each group -> Option DQuick Check:
Stacked bars = parts + total [OK]
- Confusing stacked bars with line charts
- Thinking stacked bars show individual points only
- Mixing stacked bars with pie charts
matplotlib parameter is used to stack bars on top of each other in a bar chart?Solution
Step 1: Recall bar chart parameters
Inmatplotlib,heightsets bar size,widthsets bar thickness,aligncontrols bar alignment.Step 2: Identify stacking parameter
Thebottomparameter sets where the bar starts vertically, allowing stacking on previous bars.Final Answer:
bottom -> Option BQuick Check:
Stack bars use bottom parameter [OK]
- Using height to stack bars (it sets bar size)
- Confusing width with stacking
- Using align which only shifts bars horizontally
import matplotlib.pyplot as plt labels = ['A', 'B'] values1 = [3, 5] values2 = [2, 4] plt.bar(labels, values1) plt.bar(labels, values2, bottom=values1) plt.show()
Solution
Step 1: Analyze the first bar call
The firstplt.bardraws bars of height 3 and 5 for labels 'A' and 'B'.Step 2: Analyze the second bar call with bottom
The secondplt.bardraws bars of height 2 and 4 stacked on top of the first bars usingbottom=values1.Final Answer:
Two bars per label, stacked vertically -> Option AQuick Check:
bottom stacks bars vertically [OK]
- Thinking bars appear side by side without bottom
- Assuming only one bar per label is drawn
- Believing bottom parameter causes error
import matplotlib.pyplot as plt labels = ['X', 'Y'] values1 = [4, 6] values2 = [3, 5] plt.bar(labels, values1) plt.bar(labels, values2, bottom=values2) plt.show()
Solution
Step 1: Check the bottom parameter usage
The second bar usesbottom=values2, which stacks bars on themselves, not on the first bars.Step 2: Correct stacking requires bottom=values1
To stack bars correctly, the second bars should start at the height of the first bars, sobottom=values1is needed.Final Answer:
Using bottom=values2 instead of bottom=values1 causes incorrect stacking -> Option CQuick Check:
bottom must be previous bar heights [OK]
- Setting bottom to current bar heights instead of previous
- Thinking plt.bar can only be called once
- Ignoring length matching of values
store_labels = ['Store1', 'Store2'] sales_A = [10, 15] sales_B = [5, 7] sales_C = [3, 4]
How do you create a stacked bar chart showing all three sales categories stacked for each store?
Solution
Step 1: Plot the first category normally
Plotsales_Abars starting at zero (default).Step 2: Stack second category on first
Plotsales_Bbars withbottom=sales_Ato stack on top ofsales_A.Step 3: Stack third category on sum of first two
Plotsales_Cbars withbottom=[a+b for a,b in zip(sales_A, sales_B)]to stack on top of both.Final Answer:
Plot sales_A, then sales_B with bottom=sales_A, then sales_C with bottom=sales_A + sales_B -> Option AQuick Check:
Stack each bar on sum of all below [OK]
- Stacking third category only on second, ignoring first
- Plotting all bars at bottom=0 causing overlap
- Using wrong bottom values causing incorrect stacking
Ready to prove it?
Pass the test and this pattern moves unknown
