Skip to content
svghazard-plot

Hazard curve

Does the failure rate rise, fall or stay flat with age?

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

How to read it

  1. Read x as age and y as the failure rate among units still working at that age.
  2. Falling means early failures, flat means random failures, rising means wear-out.
  3. Compare the families' curves where you have data: they often agree there and diverge beyond it.
  1. Callout 1: A falling hazard: early failures.
  2. Callout 2: A flat hazard: an exponential lifetime.
  3. Callout 3: A rising hazard: wear-out.
  4. Callout 4: One curve per family or group.

Watch out

  • These curves come from fitted families, so each one's shape is partly assumed: a Weibull cannot show a bathtub.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • The y axis stops at the 98th percentile so very early hazards do not flatten the rest.
# Hazard curve — Codesplash Chart Lab (recipe "hazard-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"]
# non-colour encodings per series (same table as the app: series are never told apart by colour alone)
SERIES_DASHES = ["-", (0, (6, 3)), (0, (1.5, 2.5)), (0, (8, 3, 2, 3)), (0, (12, 4)), (0, (3, 3)), (0, (8, 2, 2, 2, 2, 2)), (0, (1.5, 5))]

df = pd.read_csv("hazard-plot-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["shape"] = pd.Categorical(df["shape"].astype("string"), categories=["0.7", "1", "2"])

_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 = "hours"
Y = "hazard"
G = "shape"
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 = np.isfinite(v)
        t, v = t[keep], v[keep]
        yield ("all" if g is None else str(g)), t, v
fig, ax = plt.subplots(figsize=(8, 4.5))
for i, (name, t, y) in enumerate(curves()):
    ax.plot(t, y, color=PALETTE[i % len(PALETTE)], linestyle=SERIES_DASHES[i % 8], label=name)  # dash per stratum, as in the app
ax.set_xlabel(T)
ax.set_ylabel("Hazard")
ax.set_title("Hazard: failure rate among units still working", fontsize=10, loc="left")
ax.legend(frameon=False)
plt.show()

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