Bird
Raised Fist0
Matplotlibdata~10 mins

Stacked bar charts in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a stacked bar chart using matplotlib.

Matplotlib
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C']
values1 = [3, 5, 1]
values2 = [4, 2, 6]

plt.bar(labels, values1, label='Group 1')
plt.bar(labels, values2, bottom=[1], label='Group 2')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Avalues1
Bvalues2
Clabels
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values2' as bottom causes bars to overlap incorrectly.
Using 'labels' as bottom causes a type error.
2fill in blank
medium

Complete the code to add colors to the stacked bars.

Matplotlib
import matplotlib.pyplot as plt

labels = ['X', 'Y', 'Z']
values1 = [2, 7, 4]
values2 = [5, 3, 6]

plt.bar(labels, values1, color=[1], label='First')
plt.bar(labels, values2, bottom=values1, color='orange', label='Second')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
A'yellow'
B'red'
C'blue'
D'green'
Attempts:
3 left
💡 Hint
Common Mistakes
Using color without quotes causes a NameError.
Using an invalid color name causes a ValueError.
3fill in blank
hard

Fix the error in the code to correctly stack three bars.

Matplotlib
import matplotlib.pyplot as plt

labels = ['P', 'Q', 'R']
values1 = [1, 4, 3]
values2 = [2, 5, 1]
values3 = [3, 2, 4]

plt.bar(labels, values1, label='One')
plt.bar(labels, values2, bottom=[1], label='Two')
plt.bar(labels, values3, bottom=[a+b for a,b in zip(values1, values2)], label='Three')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Avalues1
Bvalues2
Cvalues1 + values2
Dlabels
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values2' as bottom for the second bar causes overlap.
Using 'labels' as bottom causes a type error.
4fill in blank
hard

Fill both blanks to create a stacked bar chart with labels and a title.

Matplotlib
import matplotlib.pyplot as plt

labels = ['Jan', 'Feb', 'Mar']
sales1 = [10, 15, 7]
sales2 = [5, 8, 12]

plt.bar(labels, sales1, color='blue', label='Online')
plt.bar(labels, sales2, bottom=[1], color='green', label=[2])
plt.xlabel('Month')
plt.title('Monthly Sales')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
Asales1
B'Offline'
C'Online'
Dsales2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sales2' as bottom causes bars to overlap incorrectly.
Using 'Online' as label for the second bar duplicates the first bar's label.
5fill in blank
hard

Fill all three blanks to create a stacked bar chart with three groups and custom colors.

Matplotlib
import matplotlib.pyplot as plt

labels = ['East', 'West', 'North']
group1 = [4, 6, 5]
group2 = [3, 7, 2]
group3 = [5, 2, 4]

plt.bar(labels, group1, color=[1], label='Group 1')
plt.bar(labels, group2, bottom=[2], color=[3], label='Group 2')
plt.bar(labels, group3, bottom=[a+b for a,b in zip(group1, group2)], color='purple', label='Group 3')
plt.legend()
plt.show()
Drag options to blanks, or click blank then click option'
A'cyan'
Bgroup1
C'magenta'
Dgroup2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'group2' as bottom for the second bar causes overlap.
Using colors without quotes causes errors.

Practice

(1/5)
1. What is the main purpose of using a stacked bar chart in data visualization?
easy
A. To create a pie chart with multiple slices
B. To display data points as individual dots on a graph
C. To plot a line connecting data points over time
D. To show how different parts contribute to the total within each group

Solution

  1. Step 1: Understand the concept of stacked bar charts

    Stacked bar charts combine multiple data parts into one bar to show totals and parts.
  2. Step 2: Identify the purpose of stacking

    Stacking helps visualize how each part contributes to the total for each group.
  3. Final Answer:

    To show how different parts contribute to the total within each group -> Option D
  4. Quick Check:

    Stacked bars = parts + total [OK]
Hint: Stacked bars add parts vertically to show totals [OK]
Common Mistakes:
  • Confusing stacked bars with line charts
  • Thinking stacked bars show individual points only
  • Mixing stacked bars with pie charts
2. Which matplotlib parameter is used to stack bars on top of each other in a bar chart?
easy
A. height
B. bottom
C. width
D. align

Solution

  1. Step 1: Recall bar chart parameters

    In matplotlib, height sets bar size, width sets bar thickness, align controls bar alignment.
  2. Step 2: Identify stacking parameter

    The bottom parameter sets where the bar starts vertically, allowing stacking on previous bars.
  3. Final Answer:

    bottom -> Option B
  4. Quick Check:

    Stack bars use bottom parameter [OK]
Hint: Use bottom to place bars on top of others [OK]
Common Mistakes:
  • Using height to stack bars (it sets bar size)
  • Confusing width with stacking
  • Using align which only shifts bars horizontally
3. What will be the output of this code snippet?
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()
medium
A. Two bars per label, stacked vertically
B. Error because bottom parameter is incorrect
C. Only one bar per label showing values2
D. Two bars per label, side by side

Solution

  1. Step 1: Analyze the first bar call

    The first plt.bar draws bars of height 3 and 5 for labels 'A' and 'B'.
  2. Step 2: Analyze the second bar call with bottom

    The second plt.bar draws bars of height 2 and 4 stacked on top of the first bars using bottom=values1.
  3. Final Answer:

    Two bars per label, stacked vertically -> Option A
  4. Quick Check:

    bottom stacks bars vertically [OK]
Hint: bottom stacks bars vertically on previous bars [OK]
Common Mistakes:
  • Thinking bars appear side by side without bottom
  • Assuming only one bar per label is drawn
  • Believing bottom parameter causes error
4. Identify the error in this code for creating a stacked bar chart:
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()
medium
A. Labels list is missing an element
B. plt.bar cannot be called twice
C. Using bottom=values2 instead of bottom=values1 causes incorrect stacking
D. values1 and values2 must be the same length

Solution

  1. Step 1: Check the bottom parameter usage

    The second bar uses bottom=values2, which stacks bars on themselves, not on the first bars.
  2. Step 2: Correct stacking requires bottom=values1

    To stack bars correctly, the second bars should start at the height of the first bars, so bottom=values1 is needed.
  3. Final Answer:

    Using bottom=values2 instead of bottom=values1 causes incorrect stacking -> Option C
  4. Quick Check:

    bottom must be previous bar heights [OK]
Hint: Stack bars using bottom=previous bar heights [OK]
Common Mistakes:
  • Setting bottom to current bar heights instead of previous
  • Thinking plt.bar can only be called once
  • Ignoring length matching of values
5. You have three categories of sales data for two stores:
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?
hard
A. Plot sales_A, then sales_B with bottom=sales_A, then sales_C with bottom=sales_A + sales_B
B. Plot sales_A, sales_B, sales_C all with bottom=0
C. Plot sales_A, then sales_B with bottom=sales_A, then sales_C with bottom=sales_B
D. Plot sales_A with bottom=sales_B, sales_B with bottom=sales_C, sales_C with bottom=sales_A

Solution

  1. Step 1: Plot the first category normally

    Plot sales_A bars starting at zero (default).
  2. Step 2: Stack second category on first

    Plot sales_B bars with bottom=sales_A to stack on top of sales_A.
  3. Step 3: Stack third category on sum of first two

    Plot sales_C bars with bottom=[a+b for a,b in zip(sales_A, sales_B)] to stack on top of both.
  4. Final Answer:

    Plot sales_A, then sales_B with bottom=sales_A, then sales_C with bottom=sales_A + sales_B -> Option A
  5. Quick Check:

    Stack each bar on sum of all below [OK]
Hint: Stack bars using cumulative sums for bottom [OK]
Common Mistakes:
  • Stacking third category only on second, ignoring first
  • Plotting all bars at bottom=0 causing overlap
  • Using wrong bottom values causing incorrect stacking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard