Skip to content

KM with CI ribbon does not show correctly with plotnine #1006

@maciekjswat

Description

@maciekjswat

Using default code

from lifelines.datasets import load_dd
data = load_dd()
kmf = KaplanMeierFitter()
kmf.fit(data["duration"], event_observed=data["observed"])
kmf.plot_survival_function()
plt.xlim(0, 20)
plt.show()

produces KM curve with CI ribbon as expected, attached.
However, combined with the following this plotnine code

km_df = kmf.survival_function_.reset_index()
km_df.columns = ["Time", "Survival"]

ci_df = kmf.confidence_interval_.reset_index()
ci_df.columns = ["Time", "Lower", "Upper"]
km_merged = km_df.merge(ci_df, on="Time")

(
p9.ggplot(km_merged, p9.aes(x="Time", y="Survival")) +
p9.geom_step(size=1.2, color="blue") +
p9.geom_ribbon(p9.aes(ymin="Lower", ymax="Upper"), alpha=0.25, fill="blue")
)

the CI ribbon is not computed/aligned correctly with the survival function, see attached.
Any idea what is going wrong?

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions