Create a Stacked Bar Chart with Labels in Python Matplotlib

I was working on a data visualization project where I needed to show the performance of different product categories across several U.S. regions. I wanted to use a stacked bar chart because it’s an excellent way to display the contribution of each category to the total value.

While Matplotlib makes it easy to create bar charts, adding labels to a stacked bar chart can be a bit tricky at first. Over the years, I’ve learned a few simple and effective techniques to make these charts both professional and easy to read.

In this Python tutorial, I’ll show you how to create a stacked bar chart with labels in Matplotlib using different methods. Each method is practical and based on my own experience as a Python developer with over a decade of hands-on work in data visualization.

What is a Stacked Bar Chart in Python?

A stacked bar chart is a type of bar graph where each bar represents a total value, divided into segments that show the contribution of different subcategories. For example, in a sales dataset, each bar could represent total sales per region, and the segments could represent different product categories.

In Matplotlib, we can easily create a stacked bar chart using the bar() function with the bottom parameter. This parameter helps us “stack” one bar on top of another.

Method 1 – Create a Basic Stacked Bar Chart in Python Matplotlib

Let’s start with a simple example to understand how stacked bars are created in Python using Matplotlib.

Before we begin, make sure you have Matplotlib installed. You can install it using:

pip install matplotlib

Now, let’s create our first stacked bar chart.

import matplotlib.pyplot as plt
import numpy as np

# Data for the chart
regions = ['East', 'West', 'North', 'South']
electronics = [320, 450, 300, 400]
furniture = [200, 250, 180, 210]
clothing = [150, 200, 120, 180]

# Create position of bars on X-axis
x = np.arange(len(regions))

# Create stacked bars
plt.bar(x, electronics, label='Electronics')
plt.bar(x, furniture, bottom=electronics, label='Furniture')
plt.bar(x, clothing, bottom=np.array(electronics) + np.array(furniture), label='Clothing')

# Add labels and title
plt.xlabel('Region')
plt.ylabel('Sales (in thousands)')
plt.title('Regional Sales Distribution by Category - USA')
plt.xticks(x, regions)
plt.legend()

# Display chart
plt.show()

You can see the output in the screenshot below.

Create Stacked Bar Chart with Labels Matplotlib

This code creates a simple stacked bar chart showing sales distribution across four U.S. regions. Each bar represents total sales, divided into three product categories.

Using the bottom parameter, we stacked the bars correctly. However, this chart doesn’t yet have labels on each segment, which we’ll add next.

Method 2 – Add Labels to a Stacked Bar Chart in Python Matplotlib

Adding labels inside or on top of each stacked segment helps users quickly understand the data. Matplotlib provides several ways to add labels, and one of the most efficient is using the bar_label() method.

Let’s modify the previous code to include labels.

import matplotlib.pyplot as plt
import numpy as np

# Data
regions = ['East', 'West', 'North', 'South']
electronics = [320, 450, 300, 400]
furniture = [200, 250, 180, 210]
clothing = [150, 200, 120, 180]

x = np.arange(len(regions))
width = 0.6

# Create figure and axes
fig, ax = plt.subplots(figsize=(8, 6))

# Plot stacked bars
bar1 = ax.bar(x, electronics, width, label='Electronics')
bar2 = ax.bar(x, furniture, width, bottom=electronics, label='Furniture')
bar3 = ax.bar(x, clothing, width, bottom=np.array(electronics) + np.array(furniture), label='Clothing')

# Add labels to each segment
ax.bar_label(bar1, label_type='center', fmt='%d')
ax.bar_label(bar2, label_type='center', fmt='%d')
ax.bar_label(bar3, label_type='center', fmt='%d')

# Customize chart
ax.set_xlabel('Region')
ax.set_ylabel('Sales (in thousands)')
ax.set_title('Stacked Bar Chart with Labels in Python Matplotlib')
ax.set_xticks(x)
ax.set_xticklabels(regions)
ax.legend()

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Create Stacked Bar Chart with Labels in Matplotlib

In this example, the bar_label() function adds numeric labels to each bar segment.
The label_type=’center’ argument places the label in the middle of each stacked section, making the chart cleaner and easier to interpret.

Method 3 – Add Percentage Labels to Stacked Bars in Python

Sometimes, instead of absolute values, we may want to display percentages to show the relative contribution of each category. This is especially helpful when comparing proportions across different groups.

Here’s how you can modify the code to display percentages.

import matplotlib.pyplot as plt
import numpy as np

# Data
regions = ['East', 'West', 'North', 'South']
electronics = np.array([320, 450, 300, 400])
furniture = np.array([200, 250, 180, 210])
clothing = np.array([150, 200, 120, 180])

# Calculate totals
totals = electronics + furniture + clothing
electronics_pct = electronics / totals * 100
furniture_pct = furniture / totals * 100
clothing_pct = clothing / totals * 100

x = np.arange(len(regions))
width = 0.6

fig, ax = plt.subplots(figsize=(8, 6))

bar1 = ax.bar(x, electronics_pct, width, label='Electronics')
bar2 = ax.bar(x, furniture_pct, width, bottom=electronics_pct, label='Furniture')
bar3 = ax.bar(x, clothing_pct, width, bottom=electronics_pct + furniture_pct, label='Clothing')

# Add percentage labels
for bars in [bar1, bar2, bar3]:
    ax.bar_label(bars, labels=[f'{h.get_height():.1f}%' for h in bars], label_type='center')

ax.set_xlabel('Region')
ax.set_ylabel('Percentage of Total Sales')
ax.set_title('Stacked Bar Chart with Percentage Labels in Python Matplotlib')
ax.set_xticks(x)
ax.set_xticklabels(regions)
ax.legend()

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Create Stacked Bar Chart with Labels in Python Matplotlib

This version shows the percentage contribution of each category, making it easier to compare proportions across regions.

Method 4 – Annotate Labels Manually for More Control

Sometimes, you may want more control over where and how labels appear. In such cases, you can manually use the text() method to place labels at specific coordinates.

import matplotlib.pyplot as plt
import numpy as np

regions = ['East', 'West', 'North', 'South']
electronics = [320, 450, 300, 400]
furniture = [200, 250, 180, 210]
clothing = [150, 200, 120, 180]

x = np.arange(len(regions))
width = 0.6

fig, ax = plt.subplots(figsize=(8, 6))

bar1 = ax.bar(x, electronics, width, label='Electronics', color='#1f77b4')
bar2 = ax.bar(x, furniture, width, bottom=electronics, label='Furniture', color='#ff7f0e')
bar3 = ax.bar(x, clothing, width, bottom=np.array(electronics) + np.array(furniture), label='Clothing', color='#2ca02c')

# Add manual labels
for i in range(len(x)):
    total = electronics[i] + furniture[i] + clothing[i]
    ax.text(x[i], total + 10, f'Total: {total}', ha='center', fontsize=9, fontweight='bold')

ax.set_xlabel('Region')
ax.set_ylabel('Sales (in thousands)')
ax.set_title('Manually Annotated Stacked Bar Chart in Python Matplotlib')
ax.set_xticks(x)
ax.set_xticklabels(regions)
ax.legend()

plt.tight_layout()
plt.show()

This approach gives you full flexibility to position labels anywhere, above bars, inside sections, or even outside the chart.

Tips for Creating Better Stacked Bar Charts in Python

Here are a few professional tips I’ve learned over the years:

  • Use contrasting colors to make each category easy to distinguish.
  • Add data labels only when necessary — too many can clutter the chart.
  • Keep the chart simple; avoid excessive gridlines or decorations.
  • Use consistent units (e.g., thousands of dollars) for clarity.
  • Always label axes and legends to make the chart self-explanatory.

Creating a stacked bar chart with labels in Python Matplotlib is a great way to visualize data composition across categories. Whether you’re analyzing sales by region, expenses by department, or survey results by demographic, stacked bar charts help communicate insights clearly.

We explored multiple methods, from basic stacking to adding custom labels and percentages, all using simple, clean Python code.

I hope you found this tutorial helpful. Try these methods with your own dataset, and you’ll see how easy it is to create professional-quality visualizations using Python and Matplotlib.

You can also read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.