Stem Plot

Cumulative Stem

Weekly values with cumulative line overlay.

Output
Cumulative Stem
Python
import matplotlib.pyplot as plt
import numpy as np

COLORS = {
    'stem': '#3B82F6',
    'cumulative': '#10B981',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

x = np.arange(1, 9)
y = [15, 22, 18, 25, 20, 28, 24, 30]
cum = np.cumsum(y)

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

# Stems
for xi, yi in zip(x, y):
    ax1.plot([xi, xi], [0, yi], color=COLORS['stem'], linewidth=2.5, alpha=0.7, zorder=2)
    ax1.scatter(xi, yi, s=100, c=COLORS['stem'], alpha=0.2, zorder=3)
    ax1.scatter(xi, yi, s=50, c=COLORS['stem'], edgecolors='white', linewidths=2, zorder=4)

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

# Cumulative line on secondary axis
ax2 = ax1.twinx()
ax2.plot(x, cum, color=COLORS['cumulative'], linewidth=2.5, zorder=5)
ax2.scatter(x, cum, s=60, c=COLORS['cumulative'], edgecolors='white', linewidths=2, zorder=6)

ax1.spines['top'].set_visible(False)
ax1.spines['left'].set_color(COLORS['stem'])
ax1.spines['bottom'].set_color(COLORS['grid'])
ax1.spines['right'].set_visible(False)

ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_color(COLORS['cumulative'])
ax2.spines['left'].set_visible(False)
ax2.spines['bottom'].set_visible(False)

ax1.tick_params(axis='y', colors=COLORS['stem'], labelsize=9, length=0, pad=8)
ax1.tick_params(axis='x', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax2.tick_params(axis='y', colors=COLORS['cumulative'], labelsize=9, length=0, pad=8)

ax1.set_xlim(0, 9)
ax1.set_ylim(0, 40)
ax2.set_ylim(0, 200)

ax1.set_xlabel('Week', fontsize=10, color=COLORS['text'], labelpad=10)
ax1.set_ylabel('Weekly', fontsize=10, color=COLORS['stem'], labelpad=10)
ax2.set_ylabel('Cumulative', fontsize=10, color=COLORS['cumulative'], labelpad=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support