ANOVA Boxplot
Fertilizer Impact on Yield ANOVA
Comparing crop yield improvements across different fertilizer types.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
np.random.seed(1313)
# Yield improvement percentage
control = np.random.normal(0, 5, 60)
nitrogen = np.random.normal(25, 8, 60)
phosphorus = np.random.normal(18, 7, 60)
potassium = np.random.normal(15, 6, 60)
npk_blend = np.random.normal(35, 10, 60)
F_stat, p_value = stats.f_oneway(control, nitrogen, phosphorus, potassium, npk_blend)
fig, ax = plt.subplots(figsize=(13, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
colors = ['#9ca3af', '#6CF527', '#F5B027', '#F5276C', '#4927F5']
data = [control, nitrogen, phosphorus, potassium, npk_blend]
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 = ['Control', 'Nitrogen (N)', 'Phosphorus (P)', 'Potassium (K)', 'NPK Blend']
# Zero baseline
ax.axhline(y=0, color='#374151', linestyle='-', alpha=0.3, linewidth=1)
# Profitability threshold
ax.axhline(y=20, color='#22c55e', linestyle='--', alpha=0.6, linewidth=1.5)
ax.text(5.45, 20, 'ROI+', fontsize=7, color='#22c55e', va='center')
# Cost per hectare
costs = ['$0', '$120', '$90', '$85', '$180']
for i, (d, cost, color) in enumerate(zip(data, costs, colors)):
ax.text(i+1, -22, f'Cost: {cost}', ha='center', fontsize=8, color=color)
# Stats header
stats_text = f"ANOVA: F={F_stat:.1f}, p<0.001 | Best ROI: NPK Blend (+{npk_blend.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, 5])
ax.set_xticklabels(labels, fontsize=10, color='#1f2937')
ax.set_ylabel('Yield Improvement (%)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Fertilizer Effect on Corn Yield\n3-Year Field Trial Results',
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(-28, 60)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More ANOVA Boxplot examples
☕