pythonsurvival-analysislifelines

Right censoring visualization of survival data


I am wondering if there is a way we can produce a graph like this

enter image description here

using survival data. For example suppose we have this dataframe

d = {'time_in_weeks': [0, 10, 20, 30, 50, 170], 'failure_status': [0, 0, 0, 0, 1, 1]}

df = pd.DataFrame(data=d)

How would we create the latter graph?


Solution

  • import seaborn as sns
    import pandas as pd
    d = {'time_in_weeks': [0, 10, 20, 30, 50, 170], 'failure_status': [0, 0, 0, 0, 1, 1]}
    

    This might be a good start

    df = pd.DataFrame(data=d)
    df['marker'] = df['failure_status'].map({0:'o',1:'x'})
    
    
    for x in df.marker.unique():
        t = df.loc[df['marker']==x]
        g = sns.scatterplot(data=t, x='time_in_weeks', y=t.index.tolist(), marker=x, s=100,color='black')
    
    for index, row in df.iterrows():
        g.hlines(y=index, xmin=0, xmax=row['time_in_weeks'], linewidth=2, color='black')
    

    enter image description here