svgpvalue-histogram
p-value histogram
Do the p-values pile up near zero, or spread flat as under the null?
How to read it
- Read bars as how many p-values fall in each bin.
- A flat histogram matches a global null.
- A spike near zero suggests real signals; other shapes suggest a problem with the tests.
- Callout 1: A spike near zero: more small p-values than chance.
- Callout 2: The dashed line: expected under the null.
Watch out
- A bump near 1 or a U-shape hints at a miscalibrated test, or a bias, rather than signal.
Defaults
- Bins
- 10
- How many bins to divide the range into.
- Range 2 to 50, step 1
Pinned in the code
- Bins are left-closed [a, b) except the last, which includes 1; the first bin is highlighted.
- Under the global null with valid p-values the bars are flat at k / bins (dashed line).
# p-value histogram — Codesplash Chart Lab (recipe "pvalue-histogram")
# controls: bins=10
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("pvalue-histogram-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")
P = "p"
BINS = 10
p = pd.to_numeric(df[P], errors="coerce").dropna()
p = p[(p >= 0) & (p <= 1)]
fig, ax = plt.subplots(figsize=(7, 4.5))
counts, edges, patches = ax.hist(p, bins=np.linspace(0, 1, BINS + 1), color=PALETTE[0], alpha=0.55, edgecolor="white")
patches[0].set_alpha(1.0) # the first bin is where real effects pile up
ax.axhline(len(p) / BINS, color=REF, linestyle="--", linewidth=1, label="expected under the global null")
ax.set_xlabel("p")
ax.set_ylabel("tests")
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads pvalue-histogram-demo.csv · app theme from the optional codesplash helper
# p-value histogram — Codesplash Chart Lab (recipe "pvalue-histogram")
# controls: bins=10
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
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("pvalue-histogram-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")
P = "p"
BINS = 10
p = pd.to_numeric(df[P], errors="coerce").dropna()
p = p[(p >= 0) & (p <= 1)]
counts, edges = np.histogram(p, bins=np.linspace(0, 1, BINS + 1))
opacity = [1.0] + [0.55] * (BINS - 1) # the first bin is where real effects pile up
fig = go.Figure(go.Bar(x=(edges[:-1] + edges[1:]) / 2, y=counts, width=edges[1] - edges[0], marker=dict(color=PALETTE[0], opacity=opacity), name="tests"))
fig.add_hline(y=len(p) / BINS, line=dict(color=REF, dash="dash", width=1))
fig.update_layout(xaxis_title="p", yaxis_title="tests", bargap=0.02)
fig.show()Needs pandas, numpy, plotly · reads pvalue-histogram-demo.csv · app theme from the optional codesplash helper
# p-value histogram — Codesplash Chart Lab (recipe "pvalue-histogram")
# controls: bins=10
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("pvalue-histogram-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
p_col <- "p"
bins <- 10
p_values <- suppressWarnings(as.numeric(df[[p_col]]))
p_values <- p_values[!is.na(p_values) & p_values >= 0 & p_values <= 1]
b <- pmin(bins - 1, floor(p_values * bins)) # left-closed bins, with 1 in the last
d <- data.frame(from = (0:(bins - 1)) / bins, n = tabulate(b + 1, nbins = bins))
d$first <- d$from == 0
p <- ggplot(d, aes(from + 0.5 / bins, n, alpha = first)) +
geom_col(width = 0.97 / bins, fill = cs_cat[1]) +
geom_hline(yintercept = length(p_values) / bins, colour = ref_col, linetype = "dashed") +
scale_alpha_manual(values = c("FALSE" = 0.55, "TRUE" = 1), guide = "none") +
labs(x = "p", y = "tests") + theme_cs
print(p)Needs ggplot2 · reads pvalue-histogram-demo.csv · app theme from the optional codesplashr helper