Line & Scatter

Timeline with Events

Time series with highlighted event markers and annotations.

Output
Timeline with Events
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'line': '#6366F1',       # Indigo
    'event': '#F43F5E',      # Rose
    'background': '#FAFBFC',
    'text_muted': '#64748B',
    'grid': '#E2E8F0',
}

# === DATA ===
np.random.seed(42)
x = np.linspace(0, 12, 120)
y = 5 + 2 * np.sin(0.5 * x) + np.cumsum(np.random.normal(0, 0.1, 120))

# Event points
events_x = [2, 5, 8, 11]
events_y = [y[20], y[50], y[80], y[110]]
event_labels = ['A', 'B', 'C', 'D']

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

# === PLOT ===
ax.plot(x, y, color=COLORS['line'], linewidth=2.5, label='Metric', zorder=2)

# Events
ax.scatter(events_x, events_y, color=COLORS['event'], s=120,
           edgecolors='white', linewidths=2, zorder=4, label='Events')

# Event labels
for ex, ey, label in zip(events_x, events_y, event_labels):
    ax.annotate(label, (ex, ey), textcoords="offset points", 
                xytext=(0, 15), ha='center', fontsize=10, fontweight='bold',
                color=COLORS['event'])

# Vertical lines for events
for ex in events_x:
    ax.axvline(x=ex, color=COLORS['event'], linewidth=1, linestyle=':', alpha=0.5, zorder=1)

# === AXES ===
ax.set_xlim(0, 12)
ax.set_ylim(0, 15)
ax.set_xlabel('Time', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax.set_ylabel('Value', 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