Stacked Bar Chart with Total Labels — Pygal

A stacked bar chart that displays multiple data series stacked on top of each other with total value labels prominently shown above each bar stack. This variant combines part-to-whole visualization with explicit numerical annotations, making it easy to read both individual segment contributions and cumulative totals at a glance. The total labels eliminate the need for mental arithmetic when comparing overall values across categories.

Stacked Bar Chart with Total Labels rendered with Pygal

Renders

Python source (Pygal)

""" anyplot.ai
bar-stacked-labeled: Stacked Bar Chart with Total Labels
Library: pygal 3.1.0 | Python 3.13.13
Quality: 88/100 | Updated: 2026-05-18
"""

import os
import sys
from pathlib import Path


# Remove the script's directory from sys.path to avoid conflict with pygal.py filename
script_dir = str(Path(__file__).parent)
sys.path = [p for p in sys.path if p != script_dir]

import pygal
from pygal.style import Style


# Theme tokens (Okabe-Ito palette, theme-adaptive chrome)
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"

# Okabe-Ito palette (first series always #009E73)
IMPRINT = ("#009E73", "#C475FD", "#4467A3", "#BD8233")

# Data - Quarterly revenue by product category (in thousands)
categories = ["Q1", "Q2", "Q3", "Q4"]
products = {
    "Electronics": [180, 95, 140, 220],
    "Furniture": [45, 130, 85, 75],
    "Clothing": [55, 60, 145, 90],
    "Accessories": [32, 45, 38, 115],
}

# Calculate totals for labels
totals = [sum(products[product][i] for product in products) for i in range(len(categories))]

# Custom style for large canvas with theme-adaptive tokens
custom_style = Style(
    background=PAGE_BG,
    plot_background=PAGE_BG,
    foreground=INK,
    foreground_strong=INK,
    foreground_subtle=INK_MUTED,
    colors=IMPRINT,
    title_font_size=28,
    label_font_size=22,
    major_label_font_size=18,
    legend_font_size=16,
    value_font_size=16,
    stroke_width=3,
)

# Create stacked bar chart with proper legend positioning
chart = pygal.StackedBar(
    width=4800,
    height=2700,
    style=custom_style,
    title="bar-stacked-labeled · Python · pygal · anyplot.ai",
    x_title="Quarter",
    y_title="Revenue ($K)",
    show_y_guides=True,
    show_x_guides=False,
    legend_at_bottom=False,  # Move legend to side to avoid truncation
    legend_at_bottom_columns=None,
    print_values=True,
    print_values_position="top",
    value_formatter=lambda x: "",  # Hide default values
    margin=80,
    margin_right=150,  # Extra space for legend on right
    margin_bottom=200,
    spacing=80,
)

# Set x-axis labels
chart.x_labels = categories

# Add data series with total labels on top
product_list = list(products.items())
for idx, (product, values) in enumerate(product_list):
    is_top = idx == len(product_list) - 1
    if is_top:
        # Top series: show total labels above each bar
        data = [{"value": v, "formatter": lambda x, i=i: f"${totals[i]}K"} for i, v in enumerate(values)]
    else:
        # Other series: no labels
        data = values
    chart.add(product, data)

# Save as PNG and HTML
chart.render_to_png(f"plot-{THEME}.png")
chart.render_to_file(f"plot-{THEME}.html")

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/bar-stacked-labeled/pygal/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "bar-stacked-labeled",
  "language": "python",
  "library": "pygal",
  "page": "https://anyplot.ai/bar-stacked-labeled/python/pygal",
  "hub": "https://anyplot.ai/bar-stacked-labeled",
  "code_json": "https://api.anyplot.ai/specs/bar-stacked-labeled/pygal/code",
  "spec_json": "https://api.anyplot.ai/specs/bar-stacked-labeled",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/bar-stacked-labeled/python/pygal/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/bar-stacked-labeled/python/pygal/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/bar-stacked-labeled/python/pygal/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/bar-stacked-labeled/python/pygal/plot-dark.html",
  "quality_score": 88.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Stacked Bar Chart with Total Labels on anyplot.ai.

Other implementations