Skip to content
svgbiplot

PCA biplot

How do rows spread on the main components, and which variables drive each direction?

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

How to read it

  1. Read points as rows on the first two components.
  2. Read arrows as variables: long arrows contribute more.
  3. Rows far along an arrow have high values for that variable.
  1. Callout 1: An arrow: this variable's direction.
  2. Callout 2: A row far along this direction.
  3. Callout 3: Rows on the other side: low values.

Watch out

  • It shows only part of the variance: check the percentages on the axes before you trust distances.

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.
  • Loadings are multiplied by one factor so the longest arrow reaches 80% of the largest score (the legend shows the factor); arrow lengths compare variables, not rows.
  • Both axes share one scale so angles between arrows read as correlations.
# PCA biplot — Codesplash Chart Lab (recipe "biplot")
# 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("biplot-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["kind"] = pd.Categorical(df["kind"].astype("string"), categories=["point", "loading"])

_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")
X = "PC1 (62%)"
Y = "PC2 (25%)"
KIND = "kind"
LAB = "label"
d = df.dropna(subset=[X, Y])
load = d[KIND].astype(str).str.lower().str.startswith("load")
pts, ld = d[~load], d[load]
reach = float(np.abs(pts[[X, Y]].to_numpy()).max()) if len(pts) else 1.0
longest = float(np.hypot(ld[X], ld[Y]).max()) if len(ld) else 1.0
scale = 0.8 * reach / longest if len(pts) else 1.0  # longest arrow reaches 80% of the score extent
fig, ax = plt.subplots(figsize=(7, 6))
ax.axhline(0, color="grey", linewidth=0.6)
ax.axvline(0, color="grey", linewidth=0.6)
ax.scatter(pts[X], pts[Y], s=16, color=PALETTE[0], alpha=0.8)
for _, row in pts.iterrows():
    ax.annotate(str(row[LAB]), (row[X], row[Y]), xytext=(3, 3), textcoords="offset points", fontsize=7, color=GREY if len(pts) > 40 else REF)
for _, row in ld.iterrows():
    ax.annotate("", xy=(row[X] * scale, row[Y] * scale), xytext=(0, 0), arrowprops=dict(arrowstyle="->", color="#ce5342", lw=1.4))
    ax.annotate(str(row[LAB]), (row[X] * scale, row[Y] * scale), xytext=(4, 4), textcoords="offset points", fontsize=9, color="#ce5342")
ax.set_xlabel(X)
ax.set_ylabel(Y)
plt.show()

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