Line & Scatter
Color-Coded Uncertainty
Error bars colored by magnitude of uncertainty.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'low': '#10B981',
'medium': '#F59E0B',
'high': '#EF4444',
'background': '#FAFBFC',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#E2E8F0',
}
# === DATA ===
x = np.arange(1, 8)
y = np.array([3.2, 4.5, 3.8, 5.2, 4.0, 5.8, 6.2])
yerr = np.array([0.3, 0.8, 0.4, 1.2, 0.5, 0.9, 0.6])
# Color based on error magnitude
def get_color(err):
if err < 0.5:
return COLORS['low']
elif err < 0.9:
return COLORS['medium']
else:
return COLORS['high']
# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === PLOT ===
for xi, yi, err in zip(x, y, yerr):
color = get_color(err)
ax.errorbar([xi], [yi], yerr=[err], fmt='o', color=color, ecolor=color,
elinewidth=2, capsize=6, capthick=2, markersize=12,
markeredgecolor='white', markeredgewidth=2)
# Legend
for label, color in [('Low (<0.5)', COLORS['low']),
('Medium (0.5-0.9)', COLORS['medium']),
('High (>0.9)', COLORS['high'])]:
ax.scatter([], [], c=color, s=100, label=label)
# === AXES ===
ax.set_xlim(0, 8)
ax.set_ylim(0, 8)
ax.set_xlabel('Sample', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax.set_ylabel('Value', fontsize=10, color=COLORS['text_muted'], labelpad=10)
# === STYLING ===
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=0.5, alpha=0.7)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12),
ncol=3, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'],
title='Uncertainty Level', title_fontsize=9)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Line & Scatter examples
☕