svgbrushablepp
Normal P–P plot
Does the centre of a distribution match a normal shape?
How to read it
- Points on the diagonal: the fitted normal's probabilities match the data's.
- An S shape around the diagonal means the data's centre differs from a normal's.
- Use the band to judge whether a departure is more than noise.
- Callout 1: Diagonal: where the data and the fitted normal agree.
- Callout 2: Sagging below the diagonal in the middle: a shape mismatch near the centre.
- Callout 3: Band: the range noise alone would allow.
Watch out
- It is least sensitive in the tails. Use a Q–Q plot when the tails matter.
Defaults
- 95% Kolmogorov band
- On
- Shade the range a correct normal fit would stay inside 95% of the time.
Pinned in the code
- x: plotting positions R `ppoints(n)`; y: Φ((x₍ᵢ₎ − x̄)/s) with s using n − 1.
- Band: ±ε around y = x with the DKW constant ε = √(ln(2/0.05)/(2n)); with estimated mean and SD it is conservative (Lilliefors) — a visual guide, not a test.
# Normal P–P plot — Codesplash Chart Lab (recipe "pp")
# controls: band=true
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
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("pp-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
def ppoints(n):
"""R ppoints(n): (i − a)/(n + 1 − 2a) with a = 3/8 for n ≤ 10, else 1/2."""
a = 3 / 8 if n <= 10 else 0.5
return (np.arange(1, n + 1) - a) / (n + 1 - 2 * a)
x = np.sort(df["repair_h"].dropna().to_numpy(dtype=float))
n = x.size
p = ppoints(n) # theoretical: R plotting positions
F = stats.norm.cdf((x - x.mean()) / x.std(ddof=1)) # empirical: Φ((x − x̄)/s), s with n − 1
fig, ax = plt.subplots(figsize=(5.5, 5))
eps = np.sqrt(np.log(2 / 0.05) / (2 * n)) # DKW constant: a visual guide (conservative with estimated mean/SD)
t = np.linspace(0, 1, 101)
ax.fill_between(t, np.clip(t - eps, 0, 1), np.clip(t + eps, 0, 1), color=PALETTE[0], alpha=0.15, linewidth=0, label="95% Kolmogorov band")
ax.plot([0, 1], [0, 1], color=plt.rcParams["text.color"], linewidth=1)
ax.scatter(p, F, s=12, color=PALETTE[0], linewidths=0)
ax.set_xlabel("Theoretical cumulative probability")
ax.set_ylabel("Empirical: Φ((repair_h − mean) / sd)")
ax.legend()
plt.show()Needs pandas, numpy, matplotlib, scipy · reads pp-demo.csv · app theme from the optional codesplash helper
# Normal P–P plot — Codesplash Chart Lab (recipe "pp")
# controls: band=true
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from scipy import stats
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 px.colors.qualitative.Plotly
df = pd.read_csv("pp-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
def ppoints(n):
"""R ppoints(n): (i − a)/(n + 1 − 2a) with a = 3/8 for n ≤ 10, else 1/2."""
a = 3 / 8 if n <= 10 else 0.5
return (np.arange(1, n + 1) - a) / (n + 1 - 2 * a)
INK = cs.palette(dark=False)["foreground"] if cs else "#131922"
x = np.sort(df["repair_h"].dropna().to_numpy(dtype=float))
n = x.size
p = ppoints(n) # theoretical: R plotting positions
F = stats.norm.cdf((x - x.mean()) / x.std(ddof=1)) # empirical: Φ((x − x̄)/s), s with n − 1
fig = go.Figure()
eps = np.sqrt(np.log(2 / 0.05) / (2 * n)) # DKW constant: a visual guide
t = np.linspace(0, 1, 101)
fig.add_scatter(x=t, y=np.clip(t - eps, 0, 1), mode="lines", line=dict(width=0), showlegend=False, hoverinfo="skip")
fig.add_scatter(x=t, y=np.clip(t + eps, 0, 1), mode="lines", line=dict(width=0), fill="tonexty", fillcolor="rgba(120, 140, 170, 0.18)", name="95% Kolmogorov band")
fig.add_scatter(x=[0, 1], y=[0, 1], mode="lines", line=dict(color=INK, width=1), showlegend=False)
fig.add_scatter(x=p, y=F, mode="markers", marker=dict(color=PALETTE[0], size=6), name="repair_h")
fig.update_layout(xaxis_title="Theoretical cumulative probability", yaxis_title="Empirical cumulative probability", width=560, height=520)
fig.show()Needs pandas, numpy, plotly, scipy · reads pp-demo.csv · app theme from the optional codesplash helper
# Normal P–P plot — Codesplash Chart Lab (recipe "pp")
# controls: band=true
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("pp-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
x <- sort(df[["repair_h"]][is.finite(df[["repair_h"]])])
n <- length(x)
pts <- data.frame(p = ppoints(n), F = pnorm((x - mean(x)) / sd(x))) # Φ((x − x̄)/s), s with n − 1
eps <- sqrt(log(2 / 0.05) / (2 * n)) # DKW constant: a visual guide (conservative with estimated mean/SD)
band <- data.frame(t = seq(0, 1, length.out = 101))
p <- ggplot(pts, aes(p, F))
p <- p + geom_ribbon(data = band, aes(x = t, ymin = pmax(t - eps, 0), ymax = pmin(t + eps, 1)), inherit.aes = FALSE, fill = cs_cat[1], alpha = 0.15)
p <- p + geom_abline(slope = 1, intercept = 0, linewidth = 0.4) +
geom_point(colour = cs_cat[1], size = 1.3) +
coord_equal() + labs(x = "Theoretical cumulative probability", y = "Empirical: Φ((repair_h − mean) / sd)") +
theme_cs
print(p)Needs ggplot2 · reads pp-demo.csv · app theme from the optional codesplashr helper
- DuckDB has no normal CDF function, so there is no geometry SQL for P–P plots.