KDE Plot

Workout Duration KDE

Density distribution of workout session lengths by activity type

Output
Workout Duration KDE
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

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

# Workout duration in minutes
cardio = np.random.gamma(5, 8, 400)
strength = np.random.gamma(4, 12, 400)
yoga = np.random.gamma(6, 10, 400)

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

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

for data, color, label in [(cardio, '#F5276C', 'Cardio'), 
                            (strength, '#4927F5', 'Strength'),
                            (yoga, '#27F5B0', 'Yoga')]:
    data_clipped = data[data < 120]
    kde = gaussian_kde(data_clipped)
    density = kde(x_range)
    ax.plot(x_range, density, color=color, linewidth=2.5, label=label)
    ax.fill_between(x_range, density, alpha=0.25, color=color)

ax.set_xlabel('Duration (minutes)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Density', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Workout Duration by Activity Type', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.set_xlim(0, 120)

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)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support