Line & Scatter

Threshold Alert Monitor

Metric monitoring with warning and critical threshold zones.

Output
Threshold Alert Monitor
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'metric': '#1E293B',
    'normal': '#10B981',
    'warning': '#F59E0B',
    'critical': '#EF4444',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
np.random.seed(42)
hours = np.arange(0, 24)
cpu_usage = 45 + 20 * np.sin(hours * 0.3) + np.random.normal(0, 8, 24)
cpu_usage = np.clip(cpu_usage, 20, 95)

warning_threshold = 70
critical_threshold = 85

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

# === THRESHOLD ZONES ===
ax.axhspan(0, warning_threshold, color=COLORS['normal'], alpha=0.08)
ax.axhspan(warning_threshold, critical_threshold, color=COLORS['warning'], alpha=0.08)
ax.axhspan(critical_threshold, 100, color=COLORS['critical'], alpha=0.08)

ax.axhline(y=warning_threshold, color=COLORS['warning'], linewidth=1.5, 
           linestyle='--', alpha=0.7)
ax.axhline(y=critical_threshold, color=COLORS['critical'], linewidth=1.5, 
           linestyle='--', alpha=0.7)

# Labels
ax.text(23.5, warning_threshold + 2, 'Warning', fontsize=8, 
        color=COLORS['warning'], ha='right')
ax.text(23.5, critical_threshold + 2, 'Critical', fontsize=8, 
        color=COLORS['critical'], ha='right')

# === METRIC LINE ===
ax.plot(hours, cpu_usage, color=COLORS['metric'], linewidth=2, zorder=3)

# Color points by zone
for h, cpu in zip(hours, cpu_usage):
    if cpu >= critical_threshold:
        color = COLORS['critical']
    elif cpu >= warning_threshold:
        color = COLORS['warning']
    else:
        color = COLORS['normal']
    ax.scatter([h], [cpu], color=color, s=50, edgecolors='white', 
               linewidths=1.5, zorder=4)

# === AXES ===
ax.set_xlim(-0.5, 24)
ax.set_ylim(0, 100)
ax.set_xticks([0, 4, 8, 12, 16, 20, 24])
ax.set_xticklabels(['00:00', '04:00', '08:00', '12:00', '16:00', '20:00', '24:00'])
ax.set_xlabel('Time', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('CPU Usage (%)', fontsize=10, color=COLORS['text'], 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)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support