Error Bar Chart

Agricultural Soil Nutrient Comparison

Soil testing comparing nutrient levels across fields relative to optimal values for crop growth.

Output
Agricultural Soil Nutrient Comparison
Python
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

# Soil nutrient data
nutrients = ['Nitrogen\n(N)', 'Phosphorus\n(P)', 'Potassium\n(K)', 'Calcium\n(Ca)', 'Magnesium\n(Mg)', 'Sulfur\n(S)']
field_a = np.array([45, 28, 180, 1200, 150, 22])
field_b = np.array([52, 22, 165, 980, 185, 18])
field_c = np.array([38, 35, 195, 1450, 120, 28])
err_a = np.array([5, 4, 15, 80, 12, 3])
err_b = np.array([6, 3, 12, 65, 15, 2])
err_c = np.array([4, 5, 18, 95, 10, 4])

# Normalize to percentage of optimal
optimal = np.array([50, 30, 200, 1200, 160, 25])
field_a_pct = field_a / optimal * 100
field_b_pct = field_b / optimal * 100
field_c_pct = field_c / optimal * 100
err_a_pct = err_a / optimal * 100
err_b_pct = err_b / optimal * 100
err_c_pct = err_c / optimal * 100

fig, ax = plt.subplots(figsize=(10, 6), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

x = np.arange(len(nutrients))
width = 0.25

ax.bar(x - width, field_a_pct, width, yerr=err_a_pct, label='Field A',
       color='#27D3F5', edgecolor='white', capsize=3,
       error_kw={'ecolor': 'white', 'elinewidth': 1.2})
ax.bar(x, field_b_pct, width, yerr=err_b_pct, label='Field B',
       color='#F5B027', edgecolor='white', capsize=3,
       error_kw={'ecolor': 'white', 'elinewidth': 1.2})
ax.bar(x + width, field_c_pct, width, yerr=err_c_pct, label='Field C',
       color='#6CF527', edgecolor='white', capsize=3,
       error_kw={'ecolor': 'white', 'elinewidth': 1.2})

ax.axhline(y=100, color='#F5276C', linestyle='--', linewidth=2, 
           label='Optimal Level', alpha=0.8)

ax.set_xlabel('Nutrient', fontsize=11, color='white', fontweight='500')
ax.set_ylabel('% of Optimal Level', fontsize=11, color='white', fontweight='500')
ax.set_title('Agricultural Soil Nutrient Comparison', fontsize=14, 
             color='white', fontweight='bold', pad=15)

ax.set_xticks(x)
ax.set_xticklabels(nutrients)
ax.legend(facecolor='#1a1a2e', edgecolor='#333', labelcolor='white', 
          fontsize=9, loc='upper right')
ax.tick_params(colors='#94a3b8', labelsize=9)
ax.set_ylim(0, 140)
ax.grid(True, axis='y', alpha=0.2, color='#4a4a6a')
for spine in ax.spines.values():
    spine.set_color('#333333')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support