KDE Plot

Delivery Time Cumulative KDE

Cumulative distribution of package delivery times by carrier

Output
Delivery Time Cumulative KDE
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

np.random.seed(777)
BG_COLOR = '#ffffff'
TEXT_COLOR = '#1f2937'
GRID_COLOR = '#e5e7eb'

# Delivery time in days
express = np.random.gamma(2, 0.5, 500) + 1
standard = np.random.gamma(3, 1.2, 500) + 2
economy = np.random.gamma(4, 1.5, 500) + 3

fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)

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

for data, color, label in [(express, '#F5276C', 'Express'), 
                            (standard, '#4927F5', 'Standard'),
                            (economy, '#F5B027', 'Economy')]:
    kde = gaussian_kde(data)
    cdf = np.array([kde.integrate_box_1d(-np.inf, x) for x in x_range])
    ax.plot(x_range, cdf, color=color, linewidth=3, label=label)

ax.axhline(0.95, color='#22c55e', linestyle='--', alpha=0.7, linewidth=2)
ax.text(15, 0.96, '95% SLA', color='#22c55e', fontsize=10)

ax.set_xlabel('Delivery Time (days)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Cumulative Probability', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Delivery Time: Cumulative Distribution', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)

ax.tick_params(colors=TEXT_COLOR, labelsize=10)
for spine in ax.spines.values():
    spine.set_color(GRID_COLOR)
ax.yaxis.grid(True, color=GRID_COLOR, linewidth=0.5, alpha=0.7)
ax.legend(facecolor=BG_COLOR, edgecolor=GRID_COLOR, labelcolor=TEXT_COLOR, fontsize=10, loc='lower right')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support