Skip to content
svgstl-decomposition

Seasonal decomposition

How much of the series is trend, how much a repeating season, and what is left?

Demo data · seed 20261101 · 96 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the top panel for the raw series.
  2. Read Trend for the long-run direction and Seasonal for the repeating shape.
  3. Read Remainder last: structure left there means the decomposition missed something.
  1. Callout 1: Observed: the series as measured.
  2. Callout 2: Trend: the long-run path.
  3. Callout 3: Seasonal: the repeating shape.
  4. Callout 4: Remainder: what's left; it should look like noise.

Watch out

  • Compare panel scales: a seasonal panel can look strong while being small relative to the trend.

Defaults

Multiplicative (remainder around 1)
Off
Treat seasonal and remainder parts as ratios around 1, not differences around 0.

Pinned in the code

  • Components are drawn as the engine returned them (statsmodels STL ≡ R stl, or R decompose); nothing is re-estimated here.
  • Each panel has its own y scale: compare shapes across panels, and sizes by reading the axes.
  • The remainder is drawn as spikes from 0 (additive) or 1 (multiplicative); gaps are the classical trend's missing ends.
# Seasonal decomposition — Codesplash Chart Lab (recipe "stl-decomposition")
# controls: multiplicative=false
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("stl-decomposition-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["month"] = pd.to_datetime(df["month"])

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
def ordered(d, x, cols):
    """Rows with the needed values, sorted by the time column (stable); row order when there is none."""
    d = d.dropna(subset=[c for c in cols if c] + ([x] if x else []))
    return d.sort_values(x, kind="stable") if x else d

def time_of(d, x):
    return d[x] if x else pd.Series(np.arange(1, len(d) + 1), index=d.index)
X = "month"
OBS = "sales"
TR = "trend"
SE = "seasonal"
RE = "remainder"
MULT = False  # remainder around 1 for a multiplicative decomposition
d = ordered(df, X, [OBS, TR, SE, RE])
t = time_of(d, X)
fig, axes = plt.subplots(4, 1, figsize=(8, 7), sharex=True)
for ax, col, name in zip(axes, [OBS, TR, SE, RE], ["Observed", "Trend", "Seasonal", "Remainder"]):
    if name == "Remainder":
        base = 1.0 if MULT else 0.0
        ax.vlines(t, base, d[col], color=PALETTE[0], alpha=0.7, linewidth=1)
        ax.axhline(base, color="grey", linewidth=0.8)
    else:
        ax.plot(t, d[col], color="black" if name == "Observed" else PALETTE[0], linewidth=1.2 if name == "Observed" else 1.6)
    ax.set_ylabel(name, fontsize=9)
axes[-1].set_xlabel("month")
fig.tight_layout()
plt.show()

Needs pandas, numpy, matplotlib · reads stl-decomposition-demo.csv · app theme from the optional codesplash helper