KDE Plot

Height Distribution by Gender

Overlapping KDE comparing height distributions between genders.

Output
Height Distribution by Gender
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(52)

# Height distributions (cm)
male = np.random.normal(175, 7, 500)
female = np.random.normal(162, 6, 500)

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

x = np.linspace(140, 200, 500)

# Male - Blue
kde_m = stats.gaussian_kde(male)
y_m = kde_m(x)
ax.fill_between(x, y_m, alpha=0.4, color='#276CF5')
ax.plot(x, y_m, color='#276CF5', linewidth=2.5, label=f'Male (mean: {np.mean(male):.0f}cm)')

# Female - Pink
kde_f = stats.gaussian_kde(female)
y_f = kde_f(x)
ax.fill_between(x, y_f, alpha=0.4, color='#F527B0')
ax.plot(x, y_f, color='#F527B0', linewidth=2.5, label=f'Female (mean: {np.mean(female):.0f}cm)')

# Overlap region
ax.fill_between(x, np.minimum(y_m, y_f), alpha=0.3, color='#4927F5', label='Overlap')

ax.set_xlabel('Height (cm)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Height Distribution by Gender', 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.legend(loc='upper right', facecolor='#1e293b', edgecolor='#334155', labelcolor='white')
ax.grid(True, alpha=0.1, color='white')
ax.set_xlim(140, 200)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support