0

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?

Snorrlaxxx
  • 168
  • 1
  • 3
  • 18

2 Answers2

2
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

Chris
  • 15,819
  • 3
  • 24
  • 37
1

lifelines has this built in: https://lifelines.readthedocs.io/en/latest/lifelines.plotting.html#lifelines.plotting.plot_lifetimes

from lifelines.plotting import plot_lifetimes
plot_lifetimes(df['time_in_weeks'], df['failure_status'], event_observed_color="k", event_censored_color="k")

enter image description here

Cam.Davidson.Pilon
  • 1,606
  • 1
  • 17
  • 31