KDE Plot
Body Weight Distribution
KDE showing body weight distribution in a population with BMI zones.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(101)
weight = np.random.normal(75, 15, 1000)
weight = weight[(weight > 40) & (weight < 140)]
kde = stats.gaussian_kde(weight)
x = np.linspace(40, 140, 500)
y = kde(x)
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
ax.fill_between(x, y, alpha=0.4, color='#F5276C')
ax.plot(x, y, color='#F5276C', linewidth=3)
ax.axvspan(40, 55, alpha=0.1, color='#27D3F5', label='Underweight')
ax.axvspan(55, 85, alpha=0.1, color='#6CF527', label='Normal')
ax.axvspan(85, 100, alpha=0.1, color='#F5B027', label='Overweight')
ax.axvspan(100, 140, alpha=0.1, color='#F5276C', label='Obese')
mean_w = np.mean(weight)
ax.axvline(mean_w, color='#374151', linestyle='--', linewidth=2)
ax.text(mean_w+2, max(y)*0.9, 'Mean: ' + str(round(mean_w, 1)) + 'kg', color='#374151', fontsize=10, fontweight='bold')
ax.set_xlabel('Weight (kg)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Body Weight Distribution', fontsize=16, color='#1f2937', fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#d1d5db')
ax.legend(loc='upper right', facecolor='#f9fafb', edgecolor='#d1d5db', labelcolor='#374151')
ax.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(40, 140)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕