Horizontal stacked bar chart in Matplotlib

A horizontal stacked bar chart displays data as horizontal bars where multiple data series are stacked on top of each other. Matplotlib's barh() method makes it easy to create these charts by using the left parameter to stack bars horizontally.

Syntax

plt.barh(y, width, left=None, height=0.8, color=None)

Parameters

  • y − The y coordinates of the bars
  • width − The width of the bars
  • left − The x coordinates of the left sides of the bars (for stacking)
  • height − The heights of the bars
  • color − The colors of the bars

Example

Let's create a horizontal stacked bar chart showing completed and pending issues over different years ?

import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Data
years = [2014, 2015, 2016, 2017, 2018, 2019]
issues_addressed = [10, 14, 0, 10, 15, 15]
issues_pending = [5, 10, 50, 2, 0, 10]

# Create horizontal bars
b1 = plt.barh(years, issues_addressed, color="red", label="Completed")
b2 = plt.barh(years, issues_pending, left=issues_addressed, color="yellow", label="Pending")

# Add labels and legend
plt.xlabel("Number of Issues")
plt.ylabel("Year")
plt.title("Issues Status by Year")
plt.legend(title="Issues", loc="upper right")

plt.show()

How It Works

The stacking effect is achieved by setting the left parameter of the second barh() call to the values of the first bar series. This positions the second bars to start where the first bars end, creating the stacked appearance.

Customizing Colors and Styling

You can customize the appearance with different colors, transparency, and edge styles ?

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True

years = [2020, 2021, 2022, 2023]
completed = [25, 30, 20, 35]
in_progress = [10, 15, 25, 12]
pending = [5, 8, 15, 3]

# Create stacked horizontal bars with custom styling
b1 = plt.barh(years, completed, color="#2E8B57", alpha=0.8, label="Completed")
b2 = plt.barh(years, in_progress, left=completed, color="#FFD700", alpha=0.8, label="In Progress")

# Calculate left position for third stack
left_pos = [c + p for c, p in zip(completed, in_progress)]
b3 = plt.barh(years, pending, left=left_pos, color="#FF6347", alpha=0.8, label="Pending")

plt.xlabel("Number of Tasks")
plt.ylabel("Year")
plt.title("Project Status Overview")
plt.legend(title="Task Status", loc="lower right")
plt.grid(axis='x', alpha=0.3)

plt.show()

Key Points

  • Use left parameter to stack bars horizontally
  • Each subsequent stack needs the cumulative sum of previous stacks as its left value
  • Colors should be distinct for better visualization
  • Add labels and legends for clarity

Conclusion

Horizontal stacked bar charts are perfect for comparing parts of a whole across categories. Use the left parameter in barh() to position bars side by side, creating the stacked effect for effective data visualization.

Updated on: 2026-03-25T22:53:57+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements