Install the lifelines library:
sudo pip3 install lifelines
Now you can use this snippet to create a basic Kaplan-Meier plot from an example dataset included with the library:
from lifelines.datasets import load_leukemia from lifelines import KaplanMeierFitter # Load example dataset df = load_leukemia() # Create model from data kmf = KaplanMeierFitter() kmf.fit(df['t'], df['Rx']) # t = Timepoints, Rx: 0=censored, 1=event # Plot model's survival curve kmf.plot()
Note that different datasets might have different names for the time column (t
in this example) and the event/censoring column (Rx
in this example)
We exported the plot using
import matplotlib.pyplot as plt plt.savefig("Kaplan-Meier-Example-Leukemias.svg")