KDE Plot

E-commerce Package Weight Distribution

KDE of package weights for shipping optimization.

Output
E-commerce Package Weight Distribution
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(117)

light = np.random.exponential(0.5, 500)
medium = np.random.normal(3, 1, 400)
heavy = np.random.normal(8, 2, 200)
weights = np.concatenate([light, medium, heavy])
weights = weights[(weights > 0) & (weights < 15)]

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

tiers = [
    (1, 'Light', '#6CF527'),
    (5, 'Standard', '#27D3F5'),
    (10, 'Heavy', '#F5B027'),
]
for val, label, color in tiers:
    ax.axvline(val, color=color, linestyle='--', linewidth=2, alpha=0.7)
    ax.text(val+0.2, max(y)*0.85, label, color=color, fontsize=9, fontweight='bold')

ax.set_xlabel('Package Weight (kg)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('E-commerce Package 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.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(0, 15)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support