Skip to content
svgmcf-plot

Mean cumulative function

For repeated events (readmissions, failures, purchases), how many events per subject accumulate over time?

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

How to read it

  1. Read the curve as the mean number of events per subject by time.
  2. Compare the curves between groups.
  3. A straight line suggests a constant event rate; bending up, an increasing one.
  1. Callout 1: Placebo accumulates more events.
  2. Callout 2: Treatment accumulates fewer.
  3. Callout 3: Band: uncertainty.

Watch out

  • Unlike survival curves, it can go above 1, because it counts repeat events.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
  • Steps hold the value until the next event time (right-continuous); bands are the engine's (Lawless–Nadeau robust variance by default).
# Mean cumulative function — Codesplash Chart Lab (recipe "mcf-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"]

df = pd.read_csv("mcf-plot-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=["placebo", "treatment"])

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)))
X = "months"
Y = "mean recurrences per patient"
LO = "ci_low"
HI = "ci_high"
G = "arm"
groups = levels_of(df[G]) if G else [None]
series = []
for name in groups:
    d = df if name is None else df[df[G].astype(str) == str(name)]
    series.append((name if name is not None else "", d.dropna(subset=[X]).sort_values(X)))
fig, ax = plt.subplots(figsize=(8, 4.5))
for i, (name, d) in enumerate(series):
    col = PALETTE[i % len(PALETTE)]
    if LO and HI:
        ax.fill_between(d[X], d[LO], d[HI], color=col, alpha=0.15, lw=0, step="post")
    ax.step(d[X], d[Y], color=col, linewidth=2, label=name or Y, where="post")
ax.set_xlabel(X)
ax.set_ylabel(Y)
if G: ax.legend(fontsize=8)
plt.show()

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