ANOVA Violin Plot
Department Productivity ANOVA
Statistical comparison of employee output metrics across business departments.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
np.random.seed(1414)
# Tasks completed per week
engineering = np.random.normal(28, 6, 60)
marketing = np.random.normal(35, 8, 60)
sales = np.random.normal(42, 12, 60)
support = np.random.normal(55, 15, 60)
hr = np.random.normal(22, 5, 60)
F_stat, p_value = stats.f_oneway(engineering, marketing, sales, support, hr)
fig, ax = plt.subplots(figsize=(13, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
colors = ['#4927F5', '#F5276C', '#6CF527', '#27D3F5', '#F5B027']
parts = ax.violinplot([engineering, marketing, sales, support, hr],
positions=[1, 2, 3, 4, 5], showmeans=True, showmedians=True, widths=0.7)
for i, pc in enumerate(parts['bodies']):
pc.set_facecolor(colors[i])
pc.set_alpha(0.6)
pc.set_edgecolor(colors[i])
pc.set_linewidth(2)
parts['cmeans'].set_color('#C82909')
parts['cmeans'].set_linewidth(2.5)
parts['cmedians'].set_color('#1f2937')
for partname in ['cbars', 'cmins', 'cmaxes']:
parts[partname].set_color('#9ca3af')
# Company average
company_avg = np.mean([engineering.mean(), marketing.mean(), sales.mean(), support.mean(), hr.mean()])
ax.axhline(y=company_avg, color='#6b7280', linestyle='--', alpha=0.7, linewidth=1.5)
ax.text(5.45, company_avg, f'Avg:{company_avg:.0f}', fontsize=8, color='#6b7280', va='center')
labels = ['Engineering', 'Marketing', 'Sales', 'Support', 'HR']
# Team size at bottom
team_sizes = [45, 22, 38, 65, 12]
for i, (size, color) in enumerate(zip(team_sizes, colors)):
ax.text(i+1, -5, f'n={size}', ha='center', fontsize=9, color=color)
# Stats at top
stats_text = f"ANOVA: F={F_stat:.2f}, p={p_value:.4f} | Note: Task complexity varies"
bbox = dict(boxstyle="round,pad=0.3", facecolor='#f0f9ff', edgecolor='#27D3F5', lw=2)
ax.text(0.5, 1.02, stats_text, transform=ax.transAxes, fontsize=10, color='#1f2937',
ha='center', va='bottom', fontfamily='monospace', bbox=bbox)
ax.set_xticks([1, 2, 3, 4, 5])
ax.set_xticklabels(labels, fontsize=11, color='#1f2937')
ax.set_ylabel('Tasks Completed per Week', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Employee Productivity by Department\nQ4 2024 Performance Metrics',
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(-12, 100)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More ANOVA Violin Plot examples
☕