Histogram

Pyramid Age Gender

Population pyramid by age and gender.

Output
Pyramid Age Gender
Python
import matplotlib.pyplot as plt
import numpy as np

COLORS = {'male': '#374151', 'female': '#EC4899', 'background': '#FFFFFF', 'text': '#1E293B', 'text_muted': '#64748B', 'grid': '#F1F5F9'}

age_groups = ['0-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70+']
male = [45, 50, 55, 60, 55, 45, 35, 25]
female = [43, 48, 58, 62, 58, 50, 40, 32]

fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

y = np.arange(len(age_groups))
ax.barh(y, [-m for m in male], height=0.8, color=COLORS['male'], label='Male')
ax.barh(y, female, height=0.8, color=COLORS['female'], label='Female')

ax.axvline(0, color=COLORS['text'], linewidth=1)
ax.set_yticks(y)
ax.set_yticklabels(age_groups)
ax.set_xticks([-60, -40, -20, 0, 20, 40, 60])
ax.set_xticklabels(['60', '40', '20', '0', '20', '40', '60'])

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_color(COLORS['grid'])
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlabel('Population (thousands)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), ncol=2, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support