Line & Scatter

Before & After Comparison

Paired comparison showing changes between two conditions.

Output
Before & After Comparison
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'before': '#94A3B8',     # Slate light
    'after': '#10B981',      # Emerald
    'connector': '#CBD5E1',  # Slate lighter
    'background': '#FAFBFC',
    'text_muted': '#64748B',
    'grid': '#E2E8F0',
}

# === DATA ===
categories = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
before = np.array([4.2, 3.8, 5.1, 2.9, 4.5, 3.2, 5.8, 4.0])
after = np.array([5.5, 4.2, 6.8, 4.1, 5.2, 4.8, 7.2, 5.5])
x = np.arange(len(categories))

# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

# === PLOT ===
# Connector lines
for i in range(len(x)):
    ax.plot([x[i], x[i]], [before[i], after[i]], 
            color=COLORS['connector'], linewidth=2, zorder=1)

# Points
ax.scatter(x, before, color=COLORS['before'], s=100, 
           edgecolors='white', linewidths=2, label='Before', zorder=3)
ax.scatter(x, after, color=COLORS['after'], s=100,
           edgecolors='white', linewidths=2, label='After', zorder=3)

# === AXES ===
ax.set_xlim(-0.5, len(categories) - 0.5)
ax.set_ylim(0, 9)
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.set_xlabel('Category', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax.set_ylabel('Score', fontsize=10, color=COLORS['text_muted'], labelpad=10)

# === STYLING ===
for spine in ['top', 'right']:
    ax.spines[spine].set_visible(False)
for spine in ['bottom', 'left']:
    ax.spines[spine].set_color(COLORS['grid'])

ax.yaxis.grid(True, color=COLORS['grid'], linewidth=0.5, alpha=0.7)
ax.xaxis.grid(False)
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

Did this help you?

Support PyLucid to keep it free & growing

Support