绘图

lifelines.plotting.add_at_risk_counts(*fitters, labels: Iterable | bool | None = None, rows_to_show=None, ypos=-0.6, xticks=None, ax=None, at_risk_count_from_start_of_period=False, **kwargs)

在生存/风险图中,添加计数以显示在每个时间点有多少个体处于风险中、被审查和观察到。

提示:你可能想在之后调用plt.tight_layout()

Parameters:
  • fitters – 一个或多个拟合器,例如 KaplanMeierFitter, WeibullFitter, NelsonAalenFitter, 等等…

  • labels – 为拟合器提供标签,默认是使用提供的拟合器标签。设置为False则不显示标签。

  • rows_to_show (list) – 一个子列表,包含 ['At risk', 'Censored', 'Events']。默认显示所有。

  • ypos – 增加正值以使表格向上移动。

  • xticks (列表) – 指定您想要评估计数的时间段(作为一个列表)。

  • at_risk_count_from_start_of_period (bool, 默认 False.) – 默认情况下,我们使用期末的风险计数。这是其他包和KMunicate建议的,但用户一直遇到同样的问题。#1383, #1316 和讨论 #1229。这进行了调整。

  • ax – 一个 matplotlib 坐标轴

Returns:

使用的轴。

Return type:

ax

示例

# First train some fitters and plot them
fig = plt.figure()
ax = plt.subplot(111)

f1 = KaplanMeierFitter()
f1.fit(data)
f1.plot(ax=ax)

f2 = KaplanMeierFitter()
f2.fit(data)
f2.plot(ax=ax)

# These calls below are equivalent
add_at_risk_counts(f1, f2)
add_at_risk_counts(f1, f2, ax=ax, fig=fig)
plt.tight_layout()

# This overrides the labels
add_at_risk_counts(f1, f2, labels=['fitter one', 'fitter two'])
plt.tight_layout()

# This hides the labels
add_at_risk_counts(f1, f2, labels=False)
plt.tight_layout()

# Only show at-risk:
add_at_risk_counts(f1, f2, rows_to_show=['At risk'])
plt.tight_layout()

参考文献

Morris TP, Jarvis CI, Cragg W, 等. 关于医学研究中Kaplan–Meier图的建议及利益相关者观点的调查:KMunicate. BMJ Open 2019;9:e030215. doi:10.1136/bmjopen-2019-030215

lifelines.plotting.cdf_plot(model, timeline=None, ax=None, **plot_kwargs)

该图比较了经验CDF(由KaplanMeier得出)与模型CDF。

Parameters:
  • model (lifelines 单变量模型)

  • 时间线 (可迭代对象)

  • ax (matplotlib 轴)

lifelines.plotting.loglogs_plot(cls, loc=None, iloc=None, show_censors=False, censor_styles=None, ax=None, **kwargs)

指定一个log(-log(SV))对log(time)的图,其中SV是估计的生存函数。

lifelines.plotting.plot_interval_censored_lifetimes(lower_bound, upper_bound, entry=None, left_truncated=False, sort_by_lower_bound=True, event_observed_color='#A60628', event_right_censored_color='#348ABD', ax=None, **kwargs)

返回区间删失数据的生存期图。

Parameters:
  • lower_bound ((n,) numpy array 或 pd.Series) – 受试者经历事件的周期开始时间。

  • upper_bound ((n,) numpy array 或 pd.Series) – 受试者经历事件的结束时间。如果该值等于 lower_bound 中的相应值,则个体的事件被观察到(未删失)。

  • entry ((n,) numpy array 或 pd.Series) – 将出生时间从 t=0 偏移。这可能是由于左截断或延迟进入研究。

  • left_truncated (boolean) – 如果提供了条目,并且数据是左截断的,这将在图中显示额外的信息以反映这一点。

  • sort_by_lower_bound (boolean) – 按 lower_bound 向量排序

  • event_observed_color (str) – 默认值: “#A60628”

  • event_right_censored_color (str) – 默认值: “#348ABD” 适用于任何具有无限上限的个体。

Return type:

ax

示例

import pandas as pd
import numpy as np
from lifelines.plotting import plot_interval_censored_lifetimes
df = pd.DataFrame({'lb':[20,15,30, 10, 20, 30], 'ub':[25, 15, np.infty, 20, 20, np.infty]})
ax = plot_interval_censored_lifetimes(lower_bound=df['lb'], upper_bound=df['ub'])
lifelines.plotting.plot_lifetimes(durations, event_observed=None, entry=None, left_truncated=False, sort_by_duration=True, event_observed_color='#A60628', event_censored_color='#348ABD', ax=None, **kwargs)

返回一个生存时间图,参见示例:https://lifelines.readthedocs.io/en/latest/Survival%20Analysis%20intro.html#Censoring

Parameters:
  • durations ((n,) numpy array 或 pd.Series) – 受试者存活的持续时间(相对于受试者的出生)。

  • event_observed ((n,) numpy array or pd.Series) – 布尔数组:如果事件被观察到则为True,否则为False。

  • entry ((n,) numpy array 或 pd.Series) – 将出生时间从 t=0 偏移。这可能是由于左截断或延迟进入研究。

  • left_truncated (boolean) – 如果提供了条目,并且数据是左截断的,这将在图中显示额外的信息以反映这一点。

  • sort_by_duration (boolean) – 按持续时间向量排序

  • event_observed_color (str) – 默认值: “#A60628”

  • event_censored_color (str) – 默认值: “#348ABD”

Return type:

ax

示例

from lifelines.datasets import load_waltons
from lifelines.plotting import plot_lifetimes
T, E = load_waltons()["T"], load_waltons()["E"]
ax = plot_lifetimes(T.loc[:50], event_observed=E.loc[:50])
lifelines.plotting.qq_plot(model, ax=None, scatter_color='k', **plot_kwargs)

生成一个经验累积分布函数(CDF)与拟合参数CDF的分位数-分位数图。远离直线y=x的大偏差可能使模型无效(尽管我们预期在尾部会有一些自然偏差)。

Parameters:
  • model (obj) – 一个已拟合的lifelines单变量参数模型,例如 WeibullFitter

  • plot_kwargs – 用于绘图的kwargs。

Returns:

使用的轴。

Return type:

ax

示例

from lifelines import *
from lifelines.plotting import qq_plot
from lifelines.datasets import load_rossi
df = load_rossi()
wf = WeibullFitter().fit(df['week'], df['arrest'])
qq_plot(wf)

注释

区间删失情况使用上下界之间的平均值。

lifelines.plotting.rmst_plot(model, model2=None, t=inf, ax=None, text_position=None, **plot_kwargs)

此函数绘制模型的生存函数及其曲线下面积(AUC)直到点t。AUC被称为限制平均生存时间(RMST)。

要比较两个模型的生存曲线之间的差异,您可以在model2中提供另一个模型。

Parameters:
  • model (lifelines.UnivariateFitter)

  • model2 (lifelines.UnivariateFitter, 可选) – 用于计算两个模型的delta RMST

  • t (float) – 期望的上限

  • ax ()

  • text_position (tuple) – 移动RMST的文本位置。

示例

from lifelines.utils import restricted_mean_survival_time
from lifelines.datasets import load_waltons
from lifelines.plotting import rmst_plot

df = load_waltons()
ix = df['group'] == 'miR-137'
T, E = df['T'], df['E']
time_limit = 50

kmf_exp = KaplanMeierFitter().fit(T[ix], E[ix], label='exp')
kmf_con = KaplanMeierFitter().fit(T[~ix], E[~ix], label='control')

ax = plt.subplot(311)
rmst_plot(kmf_exp, t=time_limit, ax=ax)

ax = plt.subplot(312)
rmst_plot(kmf_con, t=time_limit, ax=ax)

ax = plt.subplot(313)
rmst_plot(kmf_exp, model2=kmf_con, t=time_limit, ax=ax)