KDE Plot
Air Quality PM2.5 Distribution
KDE of PM2.5 particulate matter levels with health thresholds.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(113)
pm25 = np.random.lognormal(2.5, 0.7, 1000)
pm25 = pm25[pm25 < 150]
kde = stats.gaussian_kde(pm25)
x = np.linspace(0, 150, 500)
y = kde(x)
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
for i in range(len(x)-1):
if x[i] < 12:
color = '#6CF527'
elif x[i] < 35:
color = '#D3F527'
elif x[i] < 55:
color = '#F5B027'
elif x[i] < 150:
color = '#F5276C'
else:
color = '#4927F5'
ax.fill_between(x[i:i+2], y[i:i+2], alpha=0.5, color=color)
ax.plot(x, y, color='#6CF527', linewidth=3)
levels = [(12, 'Good'), (35, 'Moderate'), (55, 'Unhealthy')]
for val, label in levels:
ax.axvline(val, color='#9ca3af', linestyle='--', linewidth=1.5)
ax.text(val+2, max(y)*0.85, label, color='#6b7280', fontsize=9, fontweight='bold')
ax.set_xlabel('PM2.5 Concentration (ug/m3)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Air Quality PM2.5 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.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(0, 150)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕