Skip to content
svgperiodogram

Periodogram

Which cycle lengths carry the most variation in the series?

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

How to read it

  1. Read each peak as a cycle: higher means more variance at that frequency.
  2. Convert: period = 1 / frequency, so a peak at 0.083 is a 12-step cycle.
  3. Look for harmonics: peaks at multiples of the main frequency.
  1. Callout 1: The strongest cycle, labelled with its period.
  2. Callout 2: A harmonic of the main cycle.
  3. Callout 3: Lower peaks in the rest of the spectrum are mostly noise.

Watch out

  • A raw periodogram is noisy: isolated tall spikes occur by chance, so confirm a cycle with a test or more data.

Defaults

Season length to mark (0 = none)
0
Mark the frequency of this season and its multiples.
Range 0 to 10,000, step 1
Log scale
On
Space the axis by ratios, which suits values spanning orders of magnitude.

Pinned in the code

  • I(f_j) = |Σ (x_t − x̄) e^{−2πi f_j t}|² / n at f_j = j/n, j = 1…⌊n/2⌋ — unsmoothed and untapered (R spec.pgram with taper = 0, detrend = FALSE, demean = TRUE, times n).
  • A season of length m shows as a peak at 1/m and its harmonics k/m; a trend piles power near frequency 0.
  • Raw periodogram ordinates are noisy (each has two degrees of freedom); smooth before reading fine detail.
# Periodogram — Codesplash Chart Lab (recipe "periodogram")
# controls: period=0, log=true
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("periodogram-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_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 = None
Y = "value"
PERIOD, LOG = 0, True  # season length to mark (0 = none)
v = ordered(df, X, [Y])[Y].to_numpy(dtype=float)
n = len(v)
j = np.arange(1, n // 2 + 1)
spec = np.abs(np.fft.fft(v - v.mean())[j]) ** 2 / n  # raw periodogram at Fourier frequencies j/n
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(j / n, spec, color=PALETTE[0], linewidth=1.4)
if PERIOD > 0:
    ax.axvline(1 / PERIOD, color=REF, linestyle="--", label=f"period {PERIOD:g}")
    ax.legend(frameon=False)
if LOG:
    ax.set_yscale("log")
ax.set_xlabel("frequency (cycles per observation)")
ax.set_ylabel("spectral density")
plt.show()

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