KDE Plot

Student Test Scores Distribution

KDE analysis of exam scores showing performance distribution.

Output
Student Test Scores Distribution
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(45)

# Generate test scores with different performance groups
low_performers = np.random.normal(55, 10, 150)
average = np.random.normal(72, 8, 400)
high_performers = np.random.normal(90, 5, 200)
scores = np.concatenate([low_performers, average, high_performers])
scores = np.clip(scores, 0, 100)

kde = stats.gaussian_kde(scores)
x = np.linspace(0, 100, 500)
y = kde(x)

colors = ['#1e3a8a', '#3b82f6', '#22d3ee', '#fbbf24', '#f97316', '#dc2626']

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

# Color based on score (low=cool, high=warm)
for i in range(len(x)-1):
    score_normalized = x[i] / 100
    color_idx = int(score_normalized * (len(colors) - 1))
    ax.fill_between(x[i:i+2], y[i:i+2], alpha=0.7, color=colors[color_idx])

ax.plot(x, y, color='white', linewidth=2.5)

# Grade thresholds
grades = [(90, 'A'), (80, 'B'), (70, 'C'), (60, 'D')]
for threshold, grade in grades:
    ax.axvline(threshold, color='#e2e8f0', linestyle=':', alpha=0.5, linewidth=1)
    ax.text(threshold-2, max(y)*0.9, grade, color='#e2e8f0', fontsize=10, fontweight='bold')

mean_score = np.mean(scores)
ax.axvline(mean_score, color='#fbbf24', linestyle='--', linewidth=2)
ax.text(mean_score+2, max(y)*0.8, f'Mean: {mean_score:.1f}', color='#fbbf24', fontsize=10, fontweight='bold')

ax.set_xlabel('Score (%)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Student Test Scores 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.set_xlim(0, 100)
ax.grid(True, alpha=0.1, color='white')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support