October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Create a Stacked Bar Chart in Matplotlib (Python)

Stack series in Matplotlib by calling Axes.bar() for each one and updating a per-category bottom array with cumulative totals.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Axes.bar() once for each data series, setting bottom to the running total of the preceding series. That cumulative baseline stacks each new segment above the earlier ones. The Matplotlib 3.11.1 gallery demonstrates this approach in its official stacked bar chart example.

Build a stacked bar chart with a running bottom

Each series needs one value per category, in the same order as the category labels. Start with a zero-valued baseline, draw each series, then add its values to the baseline. The next series will begin at the cumulative height for each category.

import matplotlib.pyplot as plt
import numpy as np

labels = ["Group A", "Group B", "Group C"]
series = {
    "First": np.array([4, 3, 5]),
    "Second": np.array([2, 4, 1]),
    "Third": np.array([3, 2, 2]),
}

fig, ax = plt.subplots()
bottom = np.zeros(len(labels))

for name, values in series.items():
    ax.bar(labels, values, bottom=bottom, label=name)
    bottom += values

ax.set_ylabel("Value")
ax.set_title("Values by group")
ax.legend()
plt.show()
  1. labels supplies the categories along the horizontal axis. Each array in series must have one value per label, with values in matching order.

  2. bottom begins at zero for every category, so the first call draws the first series from the baseline.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  3. For each later call, bottom=bottom starts that series at the accumulated height. Update the array only after drawing the current series; otherwise that series would start above itself.

  4. Each label names a component in the legend. The axis label and title describe what the values and categories represent.

This is the same cumulative-baseline pattern shown in the Matplotlib 3.11.1 gallery, which describes creating a stacked bar chart using bar.

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

Check the data and read the chart carefully

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.