← All tutorials

Kaplan–Meier Survival Curve Tutorial: SPSS, R, Python and Free Online Tool

Updated November 7, 2026 · StatFAS Tutorials · Beginner to intermediate

This tutorial shows how to create a Kaplan–Meier plot and run the statistics behind the Kaplan–Meier plot in four ways: SPSS, R, Python and the free Kaplan–Meier Survival Plot Maker on statfas.com. Every script is complete and can be copied into the software. For the concepts behind the Kaplan–Meier plot, read the guide Kaplan–Meier Plot Explained: Survival Curves, Log-Rank Test and Interpretation.

Step 1: Prepare the data

One row per participant with Time (follow-up time), Event (1 = event occurred, 0 = censored) and an optional Group column with up to six groups.

Example layout:

Time_months,Event,Group
12.4,1,Control
36.0,0,Treatment
7.9,1,Treatment

Step 2: SPSS

SPSS handles Kaplan–Meier analysis well. Use Analyze > Survival > Kaplan-Meier for curves, medians and the log-rank test, and Analyze > Survival > Cox Regression for the hazard ratio.

* Variables: Time_months, Event (1 = event, 0 = censored), Group (1 = Control, 2 = Treatment).
KM Time_months BY Group
  /STATUS=Event(1)
  /PRINT TABLE MEAN
  /PLOT SURVIVAL
  /TEST LOGRANK
  /COMPARE OVERALL POOLED.

COXREG Time_months
  /STATUS=Event(1)
  /CATEGORICAL=Group
  /CONTRAST(Group)=INDICATOR(1)
  /METHOD=ENTER Group
  /PRINT=CI(95)
  /PLOT SURVIVAL.
* Menu path: Analyze > Survival > Kaplan-Meier and Analyze > Survival > Cox Regression.

Step 3: R

The survival package fits the curves, the log-rank test and the Cox model; the survminer package draws the Kaplan–Meier plot with a risk table.

library(survival); library(survminer)
d <- read.csv("survival.csv")                # columns: Time_months, Event (1 = event), Group
fit <- survfit(Surv(Time_months, Event) ~ Group, data = d)
summary(fit)$table                           # medians with 95% CI
survdiff(Surv(Time_months, Event) ~ Group, data = d)         # log-rank test
cox <- coxph(Surv(Time_months, Event) ~ Group, data = d)
summary(cox)                                 # hazard ratio, 95% CI, p
cox.zph(cox)                                 # check proportional hazards

p <- ggsurvplot(fit, data = d, pval = TRUE, risk.table = TRUE, conf.int = FALSE,
                xlab = "Time (months)", ylab = "Survival probability",
                palette = c("#d62728", "#1f77b4"), ggtheme = theme_classic())
ggsave("km.png", print(p), width = 7, height = 6, dpi = 300)

Step 4: Python

The lifelines package provides the Kaplan–Meier estimator, the log-rank test and the Cox model, and draws the number-at-risk table.

import pandas as pd
import matplotlib.pyplot as plt
from lifelines import KaplanMeierFitter, CoxPHFitter
from lifelines.statistics import multivariate_logrank_test
from lifelines.plotting import add_at_risk_counts

df = pd.read_csv("survival.csv")            # columns: Time_months, Event (1 = event), Group
fig, ax = plt.subplots(figsize=(7, 5))
fits = []
for name, d in df.groupby("Group"):
    kmf = KaplanMeierFitter().fit(d["Time_months"], d["Event"], label=str(name))
    kmf.plot_survival_function(ax=ax, ci_show=False, show_censors=True)
    fits.append(kmf)
    print(name, "median survival:", kmf.median_survival_time_)
add_at_risk_counts(*fits, ax=ax)

lr = multivariate_logrank_test(df["Time_months"], df["Group"], df["Event"])
print("log-rank chi2 =", round(lr.test_statistic, 2), "p =", lr.p_value)
ax.set_xlabel("Time (months)"); ax.set_ylabel("Survival probability")
plt.tight_layout(); plt.savefig("km.png", dpi=300)

cox = CoxPHFitter().fit(pd.get_dummies(df, columns=["Group"], drop_first=True, dtype=float),
                        duration_col="Time_months", event_col="Event")
cox.print_summary()                          # hazard ratio, 95% CI, p value

Step 5: The easy way on statfas.com

The Kaplan–Meier Survival Plot Maker needs no installation and no code. Follow these steps:

  1. Open the tool. Go to the StatFAS Kaplan–Meier Survival Plot Maker.
  2. Add data. Click Load example data to see the layout, then paste or upload Time, Event and Group columns.
  3. Check settings. Confirm the event coding (1 = event or 0 = event), the time unit and whether to show confidence bands.
  4. Read the results. The tool draws the Kaplan–Meier curves with censor marks and a number-at-risk table, and calculates medians with 95% CI, the log-rank test and the Cox hazard ratio for two groups.
  5. Download. Download the PNG or SVG, or a Word file with the figure, the tables and the APA 7 interpretation.
Try the Kaplan–Meier Survival Plot Maker free
Upload or paste data and get the Kaplan–Meier plot, the statistics and an APA 7 write-up in seconds.
Open the free Kaplan–Meier Survival Plot Maker

Step 6: Interpret and report in APA 7

Check these points before the Kaplan–Meier plot goes into a manuscript:

Example APA 7 results paragraph:

Median survival was 18.0 months (95% CI [14.7, 24.3]) in the control group and 27.9 months (95% CI [20.3, NR]) in the treatment group. A log-rank test indicated that survival differed significantly between groups, χ²(1) = 6.65, p = .010. The hazard of the event in the treatment group relative to the control group was HR = 0.55 (95% CI [0.35, 0.87]; Figure 1).

Avoid these mistakes with the Kaplan–Meier plot:

Frequently asked questions

What does a Kaplan–Meier plot show?

A Kaplan–Meier plot shows the estimated probability of remaining event-free over time for one or more groups, with censored observations marked on the curve.

What is censoring?

Censoring means that the event was not observed for a participant, for example because follow-up ended or the participant was lost. The Kaplan–Meier method uses the time observed without counting an event.

How do I compare two Kaplan–Meier curves?

Use the log-rank test for the global comparison and a Cox proportional-hazards model for a hazard ratio with a confidence interval.

What is median survival?

Median survival is the first time at which the estimated survival probability falls to 0.5 or below. If the curve never reaches 0.5, the median is not reached (NR).

Related guides

How to cite this page: StatFAS. (2026, November 7). Kaplan–Meier Survival Curve Tutorial: SPSS, R, Python and Free Online Tool. https://statfas.com/tutorials/kaplan-meier-survival-plot.html