Bar Chart

Population Pyramid

Mirrored horizontal bars for demographic comparison.

Output
Population Pyramid
Python
import matplotlib.pyplot as plt
import numpy as np

COLORS = {
    'male': '#3B82F6',
    'female': '#EC4899',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
}

age_groups = ['0-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', '70+']
male = [-5.2, -6.8, -8.5, -9.2, -8.8, -7.5, -5.2, -3.5]
female = [5.0, 6.5, 8.2, 9.0, 8.5, 7.8, 5.8, 4.2]

y = np.arange(len(age_groups))

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

ax.barh(y, male, height=0.6, color=COLORS['male'], alpha=0.85,
        edgecolor='white', linewidth=1.5, label='Male')
ax.barh(y, female, height=0.6, color=COLORS['female'], alpha=0.85,
        edgecolor='white', linewidth=1.5, label='Female')

ax.axvline(0, color=COLORS['text'], linewidth=1)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

ax.set_xticks([-10, -5, 0, 5, 10])
ax.set_xticklabels(['10%', '5%', '0', '5%', '10%'])
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)

ax.set_yticks(y)
ax.set_yticklabels(age_groups)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1),
          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