Stackplot
Age Demographics Stack
Population by age group over time.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
COLORS = {
'young': '#10B981',
'working': '#3B82F6',
'senior': '#8B5CF6',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9'
}
years = np.arange(1980, 2025, 5)
# Percentage of population
young = [35, 32, 28, 25, 22, 20, 18, 16, 15] # 0-19
working = [55, 56, 58, 60, 62, 63, 62, 60, 58] # 20-64
senior = [10, 12, 14, 15, 16, 17, 20, 24, 27] # 65+
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
ax.stackplot(years, young, working, senior,
colors=[COLORS['young'], COLORS['working'], COLORS['senior']],
alpha=0.85, labels=['0-19 Years', '20-64 Years', '65+ Years'])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlim(1980, 2020)
ax.set_ylim(0, 100)
ax.set_ylabel('Population Share (%)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), ncol=3, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Basic Charts
More Stackplot examples
☕