Stem Plot

Color-Coded Stem

Stems colored by value thresholds.

Output
Color-Coded Stem
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection

# === STYLE CONFIG ===
COLORS = {
    'high': '#10B981',
    'med': '#F59E0B',
    'low': '#EF4444',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
x = np.arange(1, 13)
y = [45, 62, 38, 75, 52, 88, 42, 65, 55, 70, 48, 82]

# Color based on value
def get_color(val):
    if val >= 70: return COLORS['high']
    elif val >= 50: return COLORS['med']
    return COLORS['low']

colors = [get_color(v) for v in y]

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

# === PLOT ===
# Draw stems manually for color control
for xi, yi, c in zip(x, y, colors):
    ax.plot([xi, xi], [0, yi], color=c, linewidth=2, alpha=0.7, zorder=2)
    ax.scatter(xi, yi, s=150, c=c, alpha=0.2, zorder=3)
    ax.scatter(xi, yi, s=80, c=c, edgecolors='white', linewidths=2, zorder=4)

# Baseline
ax.axhline(0, color=COLORS['text'], linewidth=1, zorder=1)

# Threshold lines
ax.axhline(70, color=COLORS['high'], linewidth=1, linestyle='--', alpha=0.5)
ax.axhline(50, color=COLORS['med'], linewidth=1, linestyle='--', alpha=0.5)

# === 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, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)

ax.set_xlim(0, 14)
ax.set_ylim(0, 100)
ax.set_xlabel('Month', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Performance', fontsize=10, color=COLORS['text'], labelpad=10)

# Legend
for label, color in [('High (≥70)', COLORS['high']), ('Medium (50-70)', COLORS['med']), ('Low (<50)', COLORS['low'])]:
    ax.scatter([], [], c=color, s=80, label=label, edgecolors='white', linewidths=2)
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

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support