ANOVA Boxplot
Restaurant Health Inspection ANOVA
Comparing health inspection scores across different restaurant categories.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
np.random.seed(1717)
# Health inspection score (0-100)
fast_food = np.random.normal(82, 8, 80)
casual = np.random.normal(88, 6, 80)
fine_dining = np.random.normal(94, 4, 80)
food_trucks = np.random.normal(78, 10, 80)
fast_food = np.clip(fast_food, 55, 100)
casual = np.clip(casual, 65, 100)
fine_dining = np.clip(fine_dining, 80, 100)
food_trucks = np.clip(food_trucks, 50, 98)
F_stat, p_value = stats.f_oneway(fast_food, casual, fine_dining, food_trucks)
fig, ax = plt.subplots(figsize=(12, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
colors = ['#F5B027', '#27D3F5', '#6CF527', '#F5276C']
data = [fast_food, casual, fine_dining, food_trucks]
bp = ax.boxplot(data, positions=[1, 2, 3, 4], widths=0.6, 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 = ['Fast Food', 'Casual Dining', 'Fine Dining', 'Food Trucks']
# Grade thresholds
ax.axhspan(90, 100, alpha=0.1, color='#22c55e')
ax.axhline(y=90, color='#22c55e', linestyle='--', alpha=0.6, linewidth=1.5)
ax.axhline(y=70, color='#ef4444', linestyle='--', alpha=0.6, linewidth=1.5)
ax.text(4.45, 90, 'A', fontsize=8, color='#22c55e', va='center', fontweight='bold')
ax.text(4.45, 70, 'C', fontsize=8, color='#ef4444', va='center', fontweight='bold')
# Violation rate
violations = ['2.1', '1.4', '0.6', '2.8']
for i, (d, viol, color) in enumerate(zip(data, violations, colors)):
ax.text(i+1, 48, f'μ={d.mean():.0f} | Viol:{viol}', ha='center', fontsize=8, color=color)
# Stats header
stats_text = f"ANOVA: F={F_stat:.1f}, p<0.001 | Highest Score: Fine Dining (μ={fine_dining.mean():.0f})"
bbox = dict(boxstyle="round,pad=0.3", facecolor='#f0fdf4', edgecolor='#6CF527', 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])
ax.set_xticklabels(labels, fontsize=11, color='#1f2937')
ax.set_ylabel('Health Inspection Score', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Restaurant Health Inspection Results\nCity Health Department 2024',
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(45, 102)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More ANOVA Boxplot examples
☕