KDE Plot
E-commerce Order Value Distribution
KDE of online order values with customer segment analysis.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(120)
small = np.random.exponential(25, 500)
medium = np.random.normal(80, 30, 400)
large = np.random.normal(200, 50, 200)
orders = np.concatenate([small, medium, large])
orders = orders[(orders > 0) & (orders < 400)]
kde = stats.gaussian_kde(orders)
x = np.linspace(0, 400, 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='#F5B027')
ax.plot(x, y, color='#F5B027', linewidth=3)
segments = [
(50, 'Budget', '#27D3F5'),
(100, 'Standard', '#6CF527'),
(200, 'Premium', '#4927F5'),
]
for val, label, color in segments:
ax.axvline(val, color=color, linestyle='--', linewidth=2, alpha=0.7)
ax.text(val+5, max(y)*0.85, label, color=color, fontsize=9, fontweight='bold')
median = np.median(orders)
ax.axvline(median, color='#374151', linestyle='-', linewidth=2)
ax.text(median+5, max(y)*0.7, 'Median: $' + str(int(median)), color='#374151', fontsize=10, fontweight='bold')
ax.set_xlabel('Order Value ($)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('E-commerce Order Value 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, 400)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕