Line & Scatter
Scientific Measurements
Lab-style error bar plot with reference line and annotations.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'data': '#1E293B',
'error': '#64748B',
'reference': '#EF4444',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9',
}
# === DATA ===
trials = ['T1', 'T2', 'T3', 'T4', 'T5', 'T6']
x = np.arange(len(trials))
measured = np.array([9.78, 9.82, 9.81, 9.79, 9.83, 9.80])
error = np.array([0.02, 0.03, 0.02, 0.04, 0.02, 0.03])
expected = 9.81
# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === PLOT ===
# Reference line
ax.axhline(y=expected, color=COLORS['reference'], linewidth=1.5,
linestyle='--', label='Expected (9.81 m/s²)', zorder=1)
ax.axhspan(expected - 0.02, expected + 0.02, color=COLORS['reference'],
alpha=0.1, zorder=0)
# Data points with errors
ax.errorbar(x, measured, yerr=error, fmt='o', color=COLORS['data'],
ecolor=COLORS['error'], elinewidth=2, capsize=6, capthick=2,
markersize=10, markeredgecolor='white', markeredgewidth=2,
label='Measured', zorder=2)
# Mean line
mean_val = np.mean(measured)
ax.axhline(y=mean_val, color=COLORS['data'], linewidth=1, linestyle=':',
alpha=0.5)
ax.text(5.6, mean_val + 0.01, 'Mean: {:.2f}'.format(mean_val),
fontsize=9, color=COLORS['text_muted'])
# === AXES ===
ax.set_xlim(-0.5, len(trials) - 0.5)
ax.set_ylim(9.7, 9.9)
ax.set_xticks(x)
ax.set_xticklabels(trials)
ax.set_xlabel('Trial', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax.set_ylabel('g (m/s²)', 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=1)
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=2, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Line & Scatter examples
☕