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,TreatmentStep 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 valueStep 5: The easy way on statfas.com
The Kaplan–Meier Survival Plot Maker needs no installation and no code. Follow these steps:
- Open the tool. Go to the StatFAS Kaplan–Meier Survival Plot Maker.
- Add data. Click Load example data to see the layout, then paste or upload Time, Event and Group columns.
- Check settings. Confirm the event coding (1 = event or 0 = event), the time unit and whether to show confidence bands.
- 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.
- Download. Download the PNG or SVG, or a Word file with the figure, the tables and the APA 7 interpretation.
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:
- Read the vertical axis as the estimated probability of remaining event-free at each time. The median survival time is where the curve crosses 0.5; report the 95% confidence interval too.
- The log-rank test compares the whole curves. A small p value indicates that survival differs between groups, but says nothing about the size of the difference.
- Report the hazard ratio with the 95% confidence interval from a Cox model; a hazard ratio below 1 means a lower instantaneous risk in the comparison group.
- Check the number-at-risk table. Late parts of the curve rest on very few participants, so large steps there are unreliable.
Example APA 7 results paragraph:
Avoid these mistakes with the Kaplan–Meier plot:
- Ignoring censoring and plotting the proportion alive at the end.
- Reporting a p value without a hazard ratio or confidence interval.
- Interpreting the tail of the curve where few participants remain at risk.
- Assuming proportional hazards without checking.
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