Plot guides › Chart types

Stacked bar chart

A stacked bar chart splits each bar into segments, showing both the total and how it is composed. Normalised to 100%, it compares shares across categories.

Example stacked bar chart made with matplotlib
Rendered from the code below, with example data.

When to use it

Watch out: Only the bottom segment shares a baseline, so middle segments are hard to compare across bars.

Ask for it in pltchat

Upload your file and type something like:

"stacked bar chart of energy mix by year, as percentages"

pltchat writes the loader and the plot for your columns, runs it in your browser and shows you the code.

The matplotlib code

Self-contained with example data; swap in pd.read_csv(...) or pd.read_excel(...) for your own file.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"year": ["2020", "2021", "2022", "2023", "2024"],
                   "Coal": [38, 35, 31, 27, 22],
                   "Gas": [30, 31, 30, 29, 28],
                   "Wind": [12, 14, 17, 20, 24],
                   "Solar": [6, 8, 11, 14, 18]}).set_index("year")
share = df.div(df.sum(axis=1), axis=0) * 100

fig, ax = plt.subplots(figsize=(6.4, 4))
share.plot.bar(stacked=True, ax=ax, width=0.7, rot=0)
ax.set_ylabel("Share of generation (%)")
ax.set_xlabel("")
ax.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.show()

Make this with your data

Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.

Related plots