How to create a 100% stacked Area Chart with Matplotlib?

A 100% stacked area chart displays data as percentages of the total, where each area shows the relative contribution of each category. In Matplotlib, we use stackplot() with percentage-normalized data to create this visualization.

Understanding 100% Stacked Area Charts

Unlike regular stacked area charts that show absolute values, a 100% stacked chart normalizes all values to percentages, making it easier to compare proportional relationships over time.

Creating a 100% Stacked Area Chart

Here's how to create a 100% stacked area chart showing world population distribution by continent ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

# Data: years and population by continent (in millions)
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018]

population_by_continent = {
    'Africa': [228, 284, 365, 477, 631, 814, 1044, 1275],
    'Americas': [340, 425, 519, 619, 727, 840, 943, 1006],
    'Asia': [1394, 1686, 2120, 2625, 3202, 3714, 4169, 4560],
    'Europe': [220, 253, 276, 295, 310, 303, 294, 293],
    'Oceania': [12, 15, 19, 22, 26, 31, 36, 39],
}

# Convert to numpy array for easier calculation
data = np.array(list(population_by_continent.values()))

# Calculate percentages for 100% stacked chart
totals = np.sum(data, axis=0)
percentages = (data / totals) * 100

# Create the plot
fig, ax = plt.subplots()

# Create 100% stacked area plot
ax.stackplot(years, percentages, 
             labels=population_by_continent.keys(),
             alpha=0.8)

# Customize the plot
ax.legend(loc='upper left', bbox_to_anchor=(1.02, 1))
ax.set_title('World Population Distribution by Continent (100% Stacked)', 
             fontsize=14, pad=20)
ax.set_xlabel('Year', fontsize=12)
ax.set_ylabel('Percentage of World Population (%)', fontsize=12)

# Set y-axis to show 0-100%
ax.set_ylim(0, 100)
ax.set_yticks(range(0, 101, 20))

# Add grid for better readability
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

The output shows the proportional distribution of world population across continents over time ?

A 100% stacked area chart where:
- Asia dominates with 60-70% of world population
- Europe's share decreases from ~11% to ~6%
- Africa's share increases from ~11% to ~17%
- Americas remain relatively stable around 13-15%
- Oceania maintains a small ~1% share

Key Features of the Code

  • Data normalization: Convert absolute values to percentages using (data / totals) * 100

  • stackplot(): Creates the stacked areas with alpha=0.8 for transparency

  • Y-axis range: Set to 0-100% with set_ylim(0, 100)

  • Legend positioning: Placed outside the plot area to avoid overlap

  • Grid: Added for better readability of percentage values

Alternative: Using Pandas for Data Handling

For more complex datasets, you can use Pandas to simplify percentage calculations ?

import matplotlib.pyplot as plt
import pandas as pd

# Create DataFrame
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018]
data = {
    'Africa': [228, 284, 365, 477, 631, 814, 1044, 1275],
    'Americas': [340, 425, 519, 619, 727, 840, 943, 1006],
    'Asia': [1394, 1686, 2120, 2625, 3202, 3714, 4169, 4560],
    'Europe': [220, 253, 276, 295, 310, 303, 294, 293],
    'Oceania': [12, 15, 19, 22, 26, 31, 36, 39],
}

df = pd.DataFrame(data, index=years)

# Calculate percentages
df_percent = df.div(df.sum(axis=1), axis=0) * 100

# Create stacked area plot
fig, ax = plt.subplots(figsize=(10, 6))
ax.stackplot(df_percent.index, df_percent.T, labels=df_percent.columns, alpha=0.8)

ax.legend(loc='upper left', bbox_to_anchor=(1.02, 1))
ax.set_title('World Population Distribution (100% Stacked)')
ax.set_ylabel('Percentage (%)')
ax.set_ylim(0, 100)

plt.tight_layout()
plt.show()

Conclusion

A 100% stacked area chart effectively shows proportional relationships over time by normalizing data to percentages. Use stackplot() with percentage-calculated data and set y-axis limits to 0-100% for proper visualization.

---
Updated on: 2026-03-26T00:33:31+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements