Ridgeline Plot

Exam Scores by Subject

Score distributions across different academic subjects

Output
Exam Scores by Subject
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(42)
subjects = ['Mathematics', 'Physics', 'Chemistry', 'Biology', 'English', 'History']
score_means = [72, 68, 75, 78, 82, 76]
score_stds = [12, 15, 10, 11, 8, 13]

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

colors = ['#F5276C', '#F54927', '#F5B027', '#6CF527', '#27D3F5', '#4927F5']

x = np.linspace(30, 100, 200)
overlap = 2.2

for i, (subject, mean, std, color) in enumerate(zip(subjects, score_means, score_stds, colors)):
    data = np.random.normal(mean, std, 1000)
    data = np.clip(data, 0, 100)
    kde = stats.gaussian_kde(data)
    y = kde(x) * 8
    y_offset = i * overlap
    
    ax.fill_between(x, y_offset, y + y_offset, alpha=0.85, color=color, edgecolor='white', linewidth=0.8)
    ax.text(27, y_offset + 0.3, subject, fontsize=10, color='white', va='center', ha='right', fontweight='500')

ax.set_xlim(10, 100)
ax.set_ylim(-0.5, len(subjects) * overlap + 2)
ax.set_xlabel('Score', color='white', fontsize=11, fontweight='500')
ax.set_title('Exam Score Distribution by Subject', color='white', fontsize=14, fontweight='bold', pad=20)
ax.tick_params(colors='#888888', labelsize=9)
ax.set_yticks([])
for spine in ax.spines.values():
    spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['bottom'].set_color('#333333')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support