KDE Plot

Salary Distribution by Department

KDE comparing salary distributions across different departments.

Output
Salary Distribution by Department
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(53)

# Department salaries (in thousands)
engineering = np.random.normal(95, 20, 400)
marketing = np.random.normal(70, 15, 300)
sales = np.random.lognormal(4.1, 0.4, 350)
hr = np.random.normal(60, 12, 200)

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

x = np.linspace(20, 180, 500)

departments = [
    (engineering, 'Engineering', '#27D3F5'),
    (marketing, 'Marketing', '#F54927'),
    (sales, 'Sales', '#6CF527'),
    (hr, 'HR', '#F527B0'),
]

for data, label, color in departments:
    kde = stats.gaussian_kde(data)
    y = kde(x)
    ax.fill_between(x, y, alpha=0.25, color=color)
    ax.plot(x, y, color=color, linewidth=2.5, label=f'{label} (${np.mean(data):.0f}K)')

ax.set_xlabel('Annual Salary ($K)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Salary Distribution by Department', 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(20, 180)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support