KDE Plot

Daily Commute Time Distribution

KDE showing commute duration patterns for workers.

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

np.random.seed(57)

# Commute times with different modes
walking = np.random.normal(15, 5, 200)
transit = np.random.normal(45, 15, 400)
driving = np.random.normal(35, 12, 300)
remote = np.zeros(100)  # Work from home
commute = np.concatenate([walking, transit, driving, remote])
commute = commute[commute >= 0]

kde = stats.gaussian_kde(commute[commute > 0])
x = np.linspace(0, 90, 500)
y = kde(x)

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

# Purple gradient
ax.fill_between(x, y, alpha=0.4, color='#4927F5')
ax.plot(x, y, color='#4927F5', linewidth=3)
ax.plot(x, y, color='#4927F5', linewidth=10, alpha=0.15)

# Time zones
ax.axvspan(0, 20, alpha=0.15, color='#6CF527', label='Short (<20min)')
ax.axvspan(20, 45, alpha=0.1, color='#F5B027', label='Average (20-45min)')
ax.axvspan(45, 90, alpha=0.1, color='#F5276C', label='Long (>45min)')

mean_commute = np.mean(commute[commute > 0])
ax.axvline(mean_commute, color='#27D3F5', linestyle='--', linewidth=2, label=f'Mean: {mean_commute:.0f}min')

ax.set_xlabel('Commute Time (minutes)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Daily Commute Time Distribution', fontsize=16, color='white', fontweight='bold', pad=15)

ax.tick_params(colors='white', labelsize=10)
for spine in ax.spines.values():
    spine.set_color('#334155')
ax.legend(loc='upper right', facecolor='#1e293b', edgecolor='#334155', labelcolor='white')
ax.grid(True, alpha=0.1, color='white')
ax.set_xlim(0, 90)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support