Line & Scatter

Trend with Confidence Band

Trend line with shaded confidence band.

Output
Trend with Confidence Band
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'primary': '#6366F1',    # Indigo
    'band': '#6366F1',       # Same, with alpha
    'background': '#FAFBFC',
    'text_muted': '#64748B',
    'grid': '#E2E8F0',
}

# === DATA ===
np.random.seed(42)
x = np.linspace(0, 10, 100)
y = 2 + 0.5 * x + np.sin(x)
y_upper = y + 0.3 + 0.1 * x
y_lower = y - 0.3 - 0.1 * x

x_points = np.linspace(0, 10, 15)
y_points = 2 + 0.5 * x_points + np.sin(x_points) + np.random.normal(0, 0.4, 15)

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

# === PLOT ===
ax.fill_between(x, y_lower, y_upper, 
                color=COLORS['band'], alpha=0.15,
                label='95% CI', zorder=1)

ax.plot(x, y, color=COLORS['primary'], linewidth=2.5,
        label='Trend', zorder=3)

ax.scatter(x_points, y_points, 
           color=COLORS['primary'], s=45, alpha=0.7,
           edgecolors='white', linewidths=1.5,
           label='Data', zorder=4)

# === AXES ===
ax.set_xlim(0, 10)
ax.set_ylim(0, 9)
ax.set_xlabel('X', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax.set_ylabel('Y', 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=3, 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