0
0
Matplotlibdata~5 mins

Stacked bar charts in Matplotlib

Choose your learning style9 modes available
Introduction

Stacked bar charts help us compare parts of different groups all at once. They show how each part adds up to a total.

You want to see how different categories contribute to a total in several groups.
You have data split into parts and want to compare these parts side by side.
You want to show changes in parts across different groups clearly.
You want to compare totals and parts in one simple chart.
Syntax
Matplotlib
import matplotlib.pyplot as plt

# Data for groups and parts
labels = ['Group1', 'Group2', 'Group3']
part1 = [value1, value2, value3]
part2 = [value4, value5, value6]

# Create bars for part1
plt.bar(labels, part1, label='Part 1')

# Create bars for part2 stacked on part1
plt.bar(labels, part2, bottom=part1, label='Part 2')

plt.legend()
plt.show()

The bottom parameter stacks bars on top of previous bars.

Each plt.bar call adds one part of the stack.

Examples
Basic stacked bar chart with two parts.
Matplotlib
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C']
part1 = [3, 5, 2]
part2 = [1, 2, 4]

plt.bar(labels, part1, label='Part 1')
plt.bar(labels, part2, bottom=part1, label='Part 2')
plt.legend()
plt.show()
Edge case: first part is zero, so second part bars start from zero.
Matplotlib
import matplotlib.pyplot as plt

labels = ['X', 'Y', 'Z']
part1 = [0, 0, 0]
part2 = [2, 3, 1]

plt.bar(labels, part1, label='Part 1')
plt.bar(labels, part2, bottom=part1, label='Part 2')
plt.legend()
plt.show()
Edge case: only one group with two stacked parts.
Matplotlib
import matplotlib.pyplot as plt

labels = ['Only']
part1 = [4]
part2 = [6]

plt.bar(labels, part1, label='Part 1')
plt.bar(labels, part2, bottom=part1, label='Part 2')
plt.legend()
plt.show()
Edge case: empty data, no bars shown.
Matplotlib
import matplotlib.pyplot as plt

labels = []
part1 = []
part2 = []

plt.bar(labels, part1, label='Part 1')
plt.bar(labels, part2, bottom=part1, label='Part 2')
plt.legend()
plt.show()
Sample Program

This program shows how to create a stacked bar chart with two parts. It prints the data before plotting so you see what is used.

Matplotlib
import matplotlib.pyplot as plt

# Groups and their parts
labels = ['Apples', 'Bananas', 'Cherries']
part1 = [5, 3, 4]  # Quantity of red fruits
part2 = [2, 4, 1]  # Quantity of green fruits

print('Before plotting:')
print(f'Labels: {labels}')
print(f'Part 1 values: {part1}')
print(f'Part 2 values: {part2}')

# Plot part1 bars
plt.bar(labels, part1, label='Red Fruits', color='red')

# Plot part2 bars stacked on part1
plt.bar(labels, part2, bottom=part1, label='Green Fruits', color='green')

plt.title('Stacked Bar Chart of Fruit Quantities')
plt.xlabel('Fruit Type')
plt.ylabel('Quantity')
plt.legend()
plt.show()
OutputSuccess
Important Notes

Time complexity is O(n) where n is number of groups, because each bar is drawn once.

Space complexity is O(n) for storing data arrays.

Common mistake: forgetting to use bottom causes bars to overlap instead of stack.

Use stacked bars when you want to show parts of a whole and compare totals. Use grouped bars if you want to compare parts side by side without stacking.

Summary

Stacked bar charts show how parts add up to a total for each group.

Use the bottom parameter to stack bars on top of each other.

They help compare both totals and parts across groups in one chart.