Skip to content
svgscree-plot

Scree plot

How many components carry most of the variance?

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

How to read it

  1. Read each bar as the variance one component carries.
  2. Read the line as the running total.
  3. Stop where bars flatten, or at a total share you need.
  1. Callout 1: The first component's share.
  2. Callout 2: The running total.
  3. Callout 3: Small bars: little beyond noise.

Watch out

  • Without scaling, components mostly reflect the columns with the largest units.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
  • Variance shares are proportions (eigenvalue ÷ total); the cumulative line is the engine's when sent, else the running sum.
# Scree plot — Codesplash Chart Lab (recipe "scree-plot")
# controls: defaults
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("scree-plot-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")
COMP = "component"
VAR = "share of variance"
CUM = None
d = df.dropna(subset=[COMP, VAR]).sort_values(COMP)
cum = d[CUM] if CUM else d[VAR].cumsum()
fig, ax = plt.subplots(figsize=(7, 4.5))
ax.bar(d[COMP].astype(str), d[VAR], color=PALETTE[0], label=VAR)
ax.plot(d[COMP].astype(str), cum, color=REF, marker="o", ms=4, linewidth=1.6, label="cumulative")
for x, v in zip(d[COMP].astype(str), d[VAR]):
    ax.annotate(f"{v:.0%}" if d[VAR].max() <= 1 else f"{v:.3g}", (x, v), xytext=(0, 3), textcoords="offset points", ha="center", fontsize=8)
ax.set_xlabel(COMP)
ax.set_ylabel(VAR)
ax.legend(fontsize=8)
plt.show()

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