ANOVA Violin Plot

Manufacturing Quality Control ANOVA

Statistical analysis of product defect rates across production shifts.

Output
Manufacturing Quality Control ANOVA
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

np.random.seed(654)

# Defects per 1000 units by shift
morning = np.random.normal(12.5, 3.2, 90)
afternoon = np.random.normal(15.8, 4.1, 90)
night = np.random.normal(18.2, 5.5, 90)
weekend = np.random.normal(14.1, 3.8, 90)

F_stat, p_value = stats.f_oneway(morning, afternoon, night, weekend)

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

colors = ['#F5D327', '#F5B027', '#5314E6', '#27D3F5']

parts = ax.violinplot([morning, afternoon, night, weekend], 
                       positions=[1, 2, 3, 4], showmeans=True, showmedians=True, widths=0.7)

for i, pc in enumerate(parts['bodies']):
    pc.set_facecolor(colors[i])
    pc.set_alpha(0.75)
    pc.set_edgecolor('white')
    pc.set_linewidth(1)

parts['cmeans'].set_color('#F5276C')
parts['cmeans'].set_linewidth(2.5)
parts['cmedians'].set_color('white')
for partname in ['cbars', 'cmins', 'cmaxes']:
    parts[partname].set_color('#555555')

# Control limits - labels on left side
ucl = 25
lcl = 5
ax.axhline(y=ucl, color='#ef4444', linestyle='--', alpha=0.8, linewidth=2)
ax.axhline(y=lcl, color='#22c55e', linestyle='--', alpha=0.8, linewidth=2)
ax.text(0.55, ucl+0.5, 'UCL', fontsize=9, color='#ef4444', va='bottom', fontweight='bold')
ax.text(0.55, lcl-0.5, 'LCL', fontsize=9, color='#22c55e', va='top', fontweight='bold')

# Fill between control limits
ax.fill_between([0.5, 4.5], lcl, ucl, alpha=0.08, color='#22c55e')

labels = ['Morning\n(6AM-2PM)', 'Afternoon\n(2PM-10PM)', 'Night\n(10PM-6AM)', 'Weekend']

# Stats annotation - bottom right
stats_text = f"ANOVA: F={F_stat:.2f}, p={p_value:.4f}"
bbox = dict(boxstyle="round,pad=0.3", facecolor='#1a1a2e', edgecolor='#F5D327', lw=2)
ax.text(0.98, 0.02, stats_text, transform=ax.transAxes, fontsize=10, color='white',
        ha='right', va='bottom', fontfamily='monospace', bbox=bbox)

ax.set_xticks([1, 2, 3, 4])
ax.set_xticklabels(labels, fontsize=10, color='white')
ax.set_ylabel('Defects per 1000 Units', fontsize=12, color='white', fontweight='500')
ax.set_title('Production Quality by Shift\nSix Sigma Control Analysis', 
             fontsize=14, color='white', fontweight='bold', pad=15)

ax.tick_params(colors='#888888')
for spine in ax.spines.values():
    spine.set_color('#333333')
ax.set_xlim(0.4, 4.6)
ax.set_ylim(0, 35)
ax.yaxis.grid(True, color='#1a1a2e', linewidth=0.5)
ax.set_axisbelow(True)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support