KDE Plot

Weekly Grocery Spending Distribution

KDE of household weekly grocery expenditure.

Output
Weekly Grocery Spending Distribution
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(116)

spending = np.random.gamma(8, 15, 1000)
spending = spending[spending < 300]

kde = stats.gaussian_kde(spending)
x = np.linspace(0, 300, 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='#27F5B0')
ax.plot(x, y, color='#27F5B0', linewidth=3)

mean_spend = np.mean(spending)
p25 = np.percentile(spending, 25)
p75 = np.percentile(spending, 75)

ax.axvspan(p25, p75, alpha=0.15, color='#27D3F5', label='Middle 50%')
ax.axvline(mean_spend, color='#374151', linestyle='--', linewidth=2, label='Mean: $' + str(int(mean_spend)))

ax.set_xlabel('Weekly Spending ($)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Weekly Grocery Spending 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(0, 300)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support