KDE Plot

Income Distribution Analysis

KDE plot showing household income distribution with temperature gradient fill.

Output
Income Distribution Analysis
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

income1 = np.random.normal(45000, 15000, 500)
income2 = np.random.normal(85000, 20000, 300)
income = np.concatenate([income1, income2])
income = income[income > 0]

kde = stats.gaussian_kde(income)
x = np.linspace(0, 150000, 500)
y = kde(x)

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

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

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, alpha=0.9)

for p, label in [(25, '25th'), (50, 'Median'), (75, '75th')]:
    val = np.percentile(income, p)
    ax.axvline(val, color='#fbbf24', linestyle='--', alpha=0.6, linewidth=1.5)
    val_k = val/1000
    ax.text(val, max(y)*0.95, label + ' $' + str(int(val_k)) + 'K', color='#fbbf24', 
            fontsize=9, ha='center', fontweight='bold')

ax.set_xlabel('Annual Income ($)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Household Income Distribution', fontsize=16, color='white', fontweight='bold', pad=15)

ax.tick_params(colors='white', labelsize=10)
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '$' + str(int(x/1000)) + 'K'))
for spine in ax.spines.values():
    spine.set_color('#334155')
ax.set_xlim(0, 150000)
ax.set_ylim(0, None)
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