Error Bar Chart
World Happiness Rankings
World Happiness Report scores for top-ranked countries with confidence intervals.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# Happiness data
countries = ['Finland', 'Denmark', 'Switzerland', 'Netherlands', 'Norway', 'Sweden']
happiness = np.array([7.82, 7.64, 7.57, 7.46, 7.38, 7.35])
social = np.array([0.95, 0.94, 0.92, 0.93, 0.91, 0.90])
gdp = np.array([1.45, 1.42, 1.52, 1.44, 1.48, 1.43])
err_h = np.array([0.12, 0.11, 0.13, 0.10, 0.12, 0.11])
fig, ax = plt.subplots(figsize=(10, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
x = np.arange(len(countries))
colors = ['#F5D327', '#F5B027', '#F54927', '#F5276C', '#27D3F5', '#276CF5']
bars = ax.barh(x, happiness, xerr=err_h, height=0.6, color=colors,
edgecolor='#1f2937', capsize=4,
error_kw={'ecolor': '#374151', 'elinewidth': 2})
ax.axvline(x=7.0, color='#6CF527', linestyle='--', linewidth=1.5,
label='High Happiness (7.0)', alpha=0.7)
for i, (h, e) in enumerate(zip(happiness, err_h)):
ax.text(h + e + 0.05, i, f'{h:.2f}', va='center',
color='#374151', fontsize=10, fontweight='600')
ax.set_xlabel('Happiness Score', fontsize=11, color='#374151', fontweight='500')
ax.set_ylabel('Country', fontsize=11, color='#374151', fontweight='500')
ax.set_title('World Happiness Report: Top Countries', fontsize=14,
color='#1f2937', fontweight='bold', pad=15)
ax.set_yticks(x)
ax.set_yticklabels(countries)
ax.legend(facecolor='#f8fafc', edgecolor='#d1d5db', fontsize=10, loc='lower right')
ax.tick_params(colors='#6b7280', labelsize=9)
ax.set_xlim(6.5, 8.2)
ax.grid(True, axis='x', alpha=0.3, color='#d1d5db')
for spine in ax.spines.values():
spine.set_color('#d1d5db')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Error Bar Chart examples
☕