Skip to content
svgcumhaz

Cumulative hazard

How has risk built up over time?

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

How to read it

  1. Read x as time and y as the accumulated hazard, H(t) = −log S(t).
  2. Read the slope as the failure rate at that time: steeper means failures are coming faster.
  3. A straight line through the origin is a constant rate, as an exponential lifetime has.
  1. Callout 1: A shallow stretch: few failures for the time at risk.
  2. Callout 2: A steep stretch: the rate is higher.
  3. Callout 3: One curve per group.

Watch out

  • The right end rests on few units at risk, so its jumps look larger than they deserve.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Computed from the plotted survival curve; points where S(t) = 0 are left out.
# Cumulative hazard — Codesplash Chart Lab (recipe "cumhaz")
# 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("cumhaz-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["arm"] = pd.Categorical(df["arm"].astype("string"), categories=["control", "treated"])

_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")
T = "days"
Y = "survival"
G = "arm"
def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
def curves():
    groups = levels_of(df[G]) if G else [None]
    for g in groups:
        d = df if g is None else df[df[G].astype(str) == str(g)]
        d = d.dropna(subset=[T, Y]).sort_values(T, kind="stable")
        t, v = d[T].to_numpy(dtype=float), d[Y].to_numpy(dtype=float)
        keep = v > 0
        t, v = t[keep], v[keep]
        yield ("all" if g is None else str(g)), t, -np.log(v)
fig, ax = plt.subplots(figsize=(8, 4.5))
for i, (name, t, y) in enumerate(curves()):
    ax.step(t, y, where="post", color=PALETTE[i % len(PALETTE)], label=name)
ax.set_xlabel(T)
ax.set_ylabel("H(t) = −log S(t)")
ax.set_title("Cumulative hazard: a straight line means a constant hazard", fontsize=10, loc="left")
ax.legend(frameon=False)
plt.show()

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