ANOVA Boxplot

University GPA by Major ANOVA

Comparing student GPA distributions across different academic majors.

Output
University GPA by Major ANOVA
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

np.random.seed(1111)

# GPA (0-4.0)
engineering = np.random.normal(3.2, 0.4, 80)
business = np.random.normal(3.4, 0.35, 80)
sciences = np.random.normal(3.1, 0.45, 80)
humanities = np.random.normal(3.5, 0.32, 80)
arts = np.random.normal(3.6, 0.3, 80)

engineering = np.clip(engineering, 2.0, 4.0)
business = np.clip(business, 2.0, 4.0)
sciences = np.clip(sciences, 2.0, 4.0)
humanities = np.clip(humanities, 2.2, 4.0)
arts = np.clip(arts, 2.3, 4.0)

F_stat, p_value = stats.f_oneway(engineering, business, sciences, humanities, arts)

fig, ax = plt.subplots(figsize=(13, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

colors = ['#4927F5', '#F5B027', '#6CF527', '#F5276C', '#27D3F5']
data = [engineering, business, sciences, humanities, arts]

bp = ax.boxplot(data, positions=[1, 2, 3, 4, 5], widths=0.55, patch_artist=True,
                medianprops={'color': '#1f2937', 'linewidth': 2},
                whiskerprops={'color': '#9ca3af', 'linewidth': 1.5},
                capprops={'color': '#9ca3af', 'linewidth': 1.5},
                flierprops={'marker': 'o', 'markerfacecolor': '#d1d5db', 'markersize': 4})

for patch, color in zip(bp['boxes'], colors):
    patch.set_facecolor(color)
    patch.set_alpha(0.6)
    patch.set_edgecolor(color)
    patch.set_linewidth(2)

labels = ['Engineering', 'Business', 'Sciences', 'Humanities', 'Arts']

# Dean's list threshold
ax.axhline(y=3.5, color='#22c55e', linestyle='--', alpha=0.7, linewidth=1.5)
ax.text(5.45, 3.5, "Dean's List", fontsize=7, color='#22c55e', va='center')

# Enrollment and mean
enrollment = ['2,400', '3,100', '1,800', '1,200', '850']
for i, (d, enr, color) in enumerate(zip(data, enrollment, colors)):
    ax.text(i+1, 1.85, f'μ={d.mean():.2f} | n={enr}', ha='center', fontsize=8, color=color)

# Stats header
stats_text = f"ANOVA: F={F_stat:.2f}, p={p_value:.4f} | Highest GPA: Arts (μ={arts.mean():.2f})"
bbox = dict(boxstyle="round,pad=0.3", facecolor='#f8fafc', edgecolor='#27D3F5', lw=2)
ax.text(0.5, 1.02, stats_text, transform=ax.transAxes, fontsize=9, color='#1f2937',
        ha='center', va='bottom', fontfamily='monospace', bbox=bbox)

ax.set_xticks([1, 2, 3, 4, 5])
ax.set_xticklabels(labels, fontsize=10, color='#1f2937')
ax.set_ylabel('GPA (4.0 Scale)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Academic Performance by Major\nFall 2024 Semester', 
             fontsize=14, color='#1f2937', fontweight='bold', pad=25)

ax.tick_params(colors='#374151')
for spine in ax.spines.values():
    spine.set_color('#e5e7eb')
ax.yaxis.grid(True, color='#f3f4f6', linewidth=0.8)
ax.set_axisbelow(True)
ax.set_ylim(1.8, 4.1)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support