Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use df.plot.bar(stacked=True) to create a vertical stacked bar plot from a Pandas DataFrame. Each row supplies a category, and each numeric column becomes a colored segment in that category’s bar. For a horizontal chart, use df.plot.barh(stacked=True).

Build a stacked bar plot from a DataFrame

Arrange the data so each row represents one category and each numeric column represents a series to stack. For example, this DataFrame has one row per region and one column per product:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {
        "Product A": [12, 18, 10],
        "Product B": [8, 7, 14],
        "Product C": [5, 9, 6],
    },
    index=["North", "South", "West"],
)

ax = df.plot.bar(stacked=True, figsize=(8, 5))
ax.set(title="Sales by region", xlabel="Region", ylabel="Units")
ax.legend(title="Product")
plt.tight_layout()
plt.show()

The plot uses the DataFrame index for the category labels when you omit x, and plots numeric columns as values when you omit y. Pandas’ visualization guide shows the same stacking option, and its API documents the DataFrame.plot.bar method and returned Matplotlib Axes: Pandas bar plots guide and DataFrame.plot.bar API.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the category and value columns

If your category labels are in a regular column rather than the index, pass that column through x and specify the numeric columns with y:

ax = df.plot.bar(
    x="Region",
    y=["Product A", "Product B", "Product C"],
    stacked=True,
)

x and y accept column labels or positions. If the rows and columns are reversed—series are in rows and categories in columns—transpose the DataFrame with df.T before plotting, then check that the resulting index and column labels correspond to the categories and series you intend.

Make the chart horizontal or customize its appearance

Use horizontal bars for long category names

Call barh instead of bar to make horizontal stacked bars:

ax = df.plot.barh(stacked=True)
ax.set(xlabel="Units", ylabel="Region")

The horizontal form can make long category labels easier to read. Pandas’ guide includes examples of both orientations: Pandas bar plots guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set colors and labels through the returned Axes

For the standard single plot, Pandas returns a Matplotlib Axes. Keep it in a variable such as ax to set a title and axis labels, configure the legend, format ticks, adjust limits, or add annotations. The color argument accepts a single color, a sequence, or a mapping from column names to colors. A mapping keeps each color associated with its named series:

ax = df.plot.bar(
    stacked=True,
    color={
        "Product A": "#4C78A8",
        "Product B": "#F58518",
        "Product C": "#54A24B",
    },
)
ax.legend(title="Product")

When you pass subplots=True, the API returns an array of Axes rather than a single Axes. For more specialized chart customization, you can use Matplotlib directly. See the Pandas plotting API and the Pandas visualization guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose stacking when it fits the comparison

A stacked bar shows both each category’s total and how its component series contribute to that total. The total is easy to compare across bars; interior segments are harder to compare precisely because they do not share a common baseline.

  • Use stacked bars when you need to see total magnitude and composition together.
  • Use grouped bars when precise comparisons among every series matter more than showing each category’s total as one bar.
  • Use a normalized 100% stacked chart when the main question is each series’ share of a whole rather than the original totals. Make the normalization clear, since it changes the values represented.

Stacking is not the default for bar plots, so include stacked=True explicitly. The plotting implementation documents False as the default: Pandas plotting implementation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.