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.
When to use it
- The parts add up to a meaningful total.
- You want the total first and the composition second.
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()
Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.