0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Stacked Bar Chart in Matplotlib: Simple Guide

To create a stacked bar chart in matplotlib, use the bar() function multiple times with the bottom parameter to stack bars on top of each other. Each call to bar() adds a layer, and bottom sets where the new bars start vertically.
๐Ÿ“

Syntax

The basic syntax to create stacked bars uses plt.bar() multiple times. Each call draws one set of bars. The bottom parameter shifts bars up to stack them.

  • plt.bar(x, height, bottom=previous_height):
    x is the position on x-axis.
    height is the bar height.
    bottom is the starting height to stack bars.
python
plt.bar(x, height1)
plt.bar(x, height2, bottom=height1)
plt.bar(x, height3, bottom=height1+height2)
๐Ÿ’ป

Example

This example shows how to create a stacked bar chart with three categories for three groups. Each category is stacked on top of the previous one.

python
import matplotlib.pyplot as plt
import numpy as np

# Data
labels = ['Group A', 'Group B', 'Group C']
category1 = np.array([5, 7, 3])
category2 = np.array([2, 4, 6])
category3 = np.array([3, 2, 5])

x = np.arange(len(labels))  # label locations

# Plot bars
plt.bar(x, category1, label='Category 1')
plt.bar(x, category2, bottom=category1, label='Category 2')
plt.bar(x, category3, bottom=category1 + category2, label='Category 3')

# Labels and title
plt.xticks(x, labels)
plt.ylabel('Values')
plt.title('Stacked Bar Chart Example')
plt.legend()

plt.show()
Output
A stacked bar chart with three groups on the x-axis labeled 'Group A', 'Group B', 'Group C'. Each group has three stacked bars in different colors representing 'Category 1', 'Category 2', and 'Category 3'.
โš ๏ธ

Common Pitfalls

Common mistakes when creating stacked bar charts include:

  • Not using the bottom parameter, which causes bars to overlap instead of stack.
  • Incorrectly summing the heights for bottom, leading to wrong stacking positions.
  • Using different x positions for bars that should be stacked, causing misalignment.

Always ensure the bottom parameter is the sum of all previous bar heights at each x position.

python
import matplotlib.pyplot as plt
import numpy as np

labels = ['A', 'B']
cat1 = np.array([3, 5])
cat2 = np.array([2, 4])
x = np.arange(len(labels))

# Wrong: missing bottom, bars overlap
plt.bar(x, cat1, label='Cat1')
plt.bar(x, cat2, label='Cat2')
plt.legend()
plt.title('Wrong: Bars Overlap')
plt.show()

# Correct: use bottom to stack
plt.bar(x, cat1, label='Cat1')
plt.bar(x, cat2, bottom=cat1, label='Cat2')
plt.legend()
plt.title('Correct: Stacked Bars')
plt.show()
Output
First plot shows overlapping bars for Cat1 and Cat2 at each group. Second plot shows Cat2 bars stacked on top of Cat1 bars correctly.
๐Ÿ“Š

Quick Reference

ParameterDescription
xPositions of bars on the x-axis
heightHeight of the bars (values)
bottomStarting height to stack bars on top
labelName for the legend
colorBar color (optional)
โœ…

Key Takeaways

Use plt.bar() multiple times with the bottom parameter to stack bars vertically.
The bottom parameter must be the sum of all previous bar heights at each x position.
Ensure all bars to be stacked share the same x positions for proper alignment.
Without bottom, bars will overlap instead of stacking.
Add labels and legends to make the stacked bar chart clear and readable.