Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 13

Stacked Bar Charts

Stack one series on top of another with bottom, build the running total for a third layer, and know when stacking helps or hides.

Intermediate25 min
Stacked Bar Charts hero Module 25 Lesson 13Stacked Bar Charts hero Module 25 Lesson 13

A stacked bar shows a total and its parts at the same time. The whole bar is the total; the coloured pieces are what it is made of. Matplotlib has no stack function. You draw each layer as a normal bar chart and tell it where to start with bottom.

A stacked bar chart of placements split into three teamsA stacked bar chart of placements split into three teams

Two layers

Draw the first series as usual. For the second, pass bottom=first, and every bar in the second layer starts at the top of the matching bar below it.

python
import matplotlib.pyplot as plt

years = ["2021", "2022", "2023", "2024"]
backend = [70, 40, 60, 80]
frontend = [30, 20, 25, 35]

plt.bar(years, backend, label="backend")
plt.bar(years, frontend, bottom=backend, label="frontend")
plt.title("Placements by team")
plt.xlabel("year")
plt.ylabel("placements")
plt.legend()
plt.show()

totals = [b + f for b, f in zip(backend, frontend)]
print("totals:", totals)

Output:

text
totals: [100, 60, 85, 115]

A third layer needs a running total

The third layer starts on top of the first two, so its bottom is their sum, element by element. A list comprehension over zip builds that in one line, and the pattern extends to as many layers as you need.

python
import matplotlib.pyplot as plt

years = ["2021", "2022", "2023", "2024"]
backend = [70, 40, 60, 80]
frontend = [30, 20, 25, 35]
data_team = [20, 19, 20, 18]

after_two = [b + f for b, f in zip(backend, frontend)]

plt.bar(years, backend, color="#7C3AED", label="backend")
plt.bar(years, frontend, bottom=backend, color="#2563EB", label="frontend")
plt.bar(years, data_team, bottom=after_two, color="#EC4899", label="data")
plt.title("Placements by team")
plt.ylabel("placements")
plt.legend()
plt.show()

print("running starts for the third layer:", after_two)

Output:

text
running starts for the third layer: [100, 60, 85, 115]

Stacking sideways

barh stacks too, but the argument is called left rather than bottom, because the bars now grow to the right. Everything else is the same.

python
import matplotlib.pyplot as plt

years = ["2021", "2022", "2023", "2024"]
backend = [70, 40, 60, 80]
frontend = [30, 20, 25, 35]
data_team = [20, 19, 20, 18]
after_two = [b + f for b, f in zip(backend, frontend)]

plt.barh(years, backend, color="#7C3AED", label="backend")
plt.barh(years, frontend, left=backend, color="#2563EB", label="frontend")
plt.barh(years, data_team, left=after_two, color="#EC4899", label="data")
plt.xlabel("placements")
plt.legend()
plt.show()

print("bottom becomes left when the bars lie down")

Output:

text
bottom becomes left when the bars lie down

When stacking hides the answer

Only the bottom layer sits on a flat line, so only the bottom layer is easy to compare across categories. Every layer above it starts somewhere different. If the question is how did the data team change, draw the groups side by side instead by shifting their positions.

python
import matplotlib.pyplot as plt

years = [2021, 2022, 2023, 2024]
backend = [70, 40, 60, 80]
frontend = [30, 20, 25, 35]
width = 0.4

left_positions = [year - width / 2 for year in years]
right_positions = [year + width / 2 for year in years]

plt.bar(left_positions, backend, width=width, color="#7C3AED", label="backend")
plt.bar(right_positions, frontend, width=width, color="#2563EB", label="frontend")
plt.xticks(years)
plt.title("Side by side, both on the same baseline")
plt.legend()
plt.show()

print("use stacked for totals, grouped for comparisons")

Output:

text
use stacked for totals, grouped for comparisons
A plain bar chart where every bar starts from the same lineA plain bar chart where every bar starts from the same line

Key takeaways

  • Stack by passing bottom= the layer underneath

  • A third layer needs the running total of the first two

  • barh stacks with left= instead of bottom=

  • Stacked shows totals; grouped compares parts fairly

  • Next: Scatter Plots

  • Previous: Horizontal Bar Charts

Loading playground…

Check your understanding

Stacked Bar Charts Quiz

Which argument stacks one bar series on another?

1 / 10