KDE Plot

Population Age Distribution

KDE visualization of population age demographics with heat gradient.

Output
Population Age Distribution
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(43)

# Generate realistic age distribution
ages = np.concatenate([
    np.random.normal(25, 8, 400),
    np.random.normal(45, 12, 500),
    np.random.normal(70, 10, 300)
])
ages = ages[(ages >= 0) & (ages <= 100)]

kde = stats.gaussian_kde(ages)
x = np.linspace(0, 100, 500)
y = kde(x)

colors = ['#1e3a8a', '#3b82f6', '#22d3ee', '#fbbf24', '#f97316', '#dc2626']

fig, ax = plt.subplots(figsize=(12, 6), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

# Gradient fill based on density
for i in range(len(x)-1):
    color_idx = int((y[i] / max(y)) * (len(colors) - 1))
    ax.fill_between(x[i:i+2], y[i:i+2], alpha=0.7, color=colors[min(color_idx, len(colors)-1)])

ax.plot(x, y, color='white', linewidth=2.5)

# Age group annotations
ax.axvspan(0, 18, alpha=0.1, color='#3b82f6', label='Youth')
ax.axvspan(18, 65, alpha=0.1, color='#22d3ee', label='Working Age')
ax.axvspan(65, 100, alpha=0.1, color='#f97316', label='Senior')

ax.set_xlabel('Age (Years)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Population Age Distribution', fontsize=16, color='white', fontweight='bold', pad=15)

ax.tick_params(colors='white', labelsize=10)
for spine in ax.spines.values():
    spine.set_color('#334155')
ax.set_xlim(0, 100)
ax.legend(loc='upper right', facecolor='#1e293b', edgecolor='#334155', labelcolor='white')
ax.grid(True, alpha=0.1, color='white')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support