Histogram

Test Score Distribution

Exam results with pass rate.

Output
Test Score Distribution
Python
import matplotlib.pyplot as plt
import numpy as np

COLORS = {'fail': '#374151', 'pass': '#10B981', 'threshold': '#DC2626', 'background': '#FFFFFF', 'text': '#1E293B', 'text_muted': '#64748B', 'grid': '#F1F5F9'}

np.random.seed(42)
scores = np.clip(np.random.normal(72, 15, 500), 0, 100)

fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

pass_score = 60
counts, bins, patches = ax.hist(scores, bins=25, edgecolor='white', linewidth=0.5)
for i, patch in enumerate(patches):
    bin_center = (bins[i] + bins[i+1]) / 2
    patch.set_facecolor(COLORS['pass'] if bin_center >= pass_score else COLORS['fail'])
    patch.set_alpha(0.85)

ax.axvline(pass_score, color=COLORS['threshold'], linewidth=2, linestyle='--', label=f'Pass: {pass_score}%')
pass_rate = (scores >= pass_score).sum() / len(scores) * 100
ax.text(0.97, 0.95, f'{pass_rate:.0f}% passed', transform=ax.transAxes, fontsize=10, fontweight='bold',
        va='top', ha='right', color=COLORS['pass'])

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlabel('Score (%)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Students', fontsize=10, color=COLORS['text'], labelpad=10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support