KDE Plot

Delivery Time Distribution

KDE comparing delivery times across shipping methods.

Output
Delivery Time Distribution
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(107)

express = np.random.gamma(2, 0.5, 400)
standard = np.random.gamma(3, 1.5, 500)
economy = np.random.gamma(4, 2, 300)

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

x = np.linspace(0, 20, 500)

methods = [
    (express, 'Express', '#27F5B0'),
    (standard, 'Standard', '#F5B027'),
    (economy, 'Economy', '#F5276C'),
]

for data, label, color in methods:
    kde = stats.gaussian_kde(data)
    y = kde(x)
    mean_val = np.mean(data)
    ax.fill_between(x, y, alpha=0.3, color=color)
    ax.plot(x, y, color=color, linewidth=2.5, label=label + ' (' + str(round(mean_val, 1)) + ' days)')

ax.set_xlabel('Delivery Time (days)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Delivery Time by Shipping Method', 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, 20)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support