KDE Plot

Employee Tenure Distribution

KDE showing years of service across an organization.

Output
Employee Tenure Distribution
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(114)

tenure = np.random.exponential(4, 1000)
tenure = tenure[tenure < 25]

kde = stats.gaussian_kde(tenure)
x = np.linspace(0, 25, 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='#276CF5')
ax.plot(x, y, color='#276CF5', linewidth=3)

ax.axvspan(0, 2, alpha=0.1, color='#27D3F5', label='New (0-2 yrs)')
ax.axvspan(2, 5, alpha=0.1, color='#6CF527', label='Established (2-5 yrs)')
ax.axvspan(5, 10, alpha=0.1, color='#F5B027', label='Senior (5-10 yrs)')
ax.axvspan(10, 25, alpha=0.1, color='#4927F5', label='Veteran (10+ yrs)')

median = np.median(tenure)
ax.axvline(median, color='#374151', linestyle='--', linewidth=2)
ax.text(median+0.5, max(y)*0.85, 'Median: ' + str(round(median, 1)) + ' yrs', color='#374151', fontsize=10, fontweight='bold')

ax.set_xlabel('Years of Service', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Employee Tenure 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.legend(loc='upper right', facecolor='#f9fafb', edgecolor='#d1d5db', labelcolor='#374151')
ax.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(0, 25)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support