gpt4 book ai didi

python - 绘图图表上的辅助/平行 X 轴 (python)

转载 作者:行者123 更新时间:2023-12-01 08:26:15 30 4
gpt4 key购买 nike

我需要在 Kaplan Meier 图上呈现 at_risk 数字。

最终结果应该与此类似:

enter image description here

我在渲染时遇到的问题是 No。处于危险中的患者数量位于图表底部。此处显示的值对应于 x 轴上的值。因此本质上,它就像与 X 平行渲染的 Y 轴。

我一直在尝试复制此处找到的多轴( https://plot.ly/python/multiple-axes/ ),但没有成功,并且还尝试使用子图并隐藏除 X 轴之外的所有内容,但其值与上面的图表不对齐。

最好的方法是什么?

最佳答案

您可以使用子图,使用 Plotly 绘制处于危险中的患者的 Kaplan-Meier 生存图。第一个图显示了生存率,第二个图是散点图,其中仅显示文本,即不显示标记。

这两个图具有相同的 y 轴,并且有风险的患者绘制在各自的 x 值处。

更多示例如下: https://github.com/Ashafix/Kaplan-Meier_Plotly

示例 1 - 女性和男性患者的肺癌

import pandas as pd
import lifelines
import plotly
import numpy as np
plotly.offline.init_notebook_mode()

df = pd.read_csv('http://www-eio.upc.edu/~pau/cms/rdata/csv/survival/lung.csv')

fig = plotly.tools.make_subplots(rows=2, cols=1, print_grid=False)
kmfs = []

dict_sex = {1: 'Male', 2: 'Female'}

steps = 5 # the number of time points where number of patients at risk which should be shown

x_min = 0 # min value in x-axis, used to make sure that both plots have the same range
x_max = 0 # max value in x-axis

for sex in df.sex.unique():
T = df[df.sex == sex]["time"]
E = df[df.sex == sex]["status"]
kmf = lifelines.KaplanMeierFitter()

kmf.fit(T, event_observed=E)
kmfs.append(kmf)
x_max = max(x_max, max(kmf.event_table.index))
x_min = min(x_min, min(kmf.event_table.index))
fig.append_trace(plotly.graph_objs.Scatter(x=kmf.survival_function_.index,
y=kmf.survival_function_.values.flatten(),
name=dict_sex[sex]),
1, 1)


for s, sex in enumerate(df.sex.unique()):
x = []
kmf = kmfs[s].event_table
for i in range(0, int(x_max), int(x_max / (steps - 1))):
x.append(kmf.iloc[np.abs(kmf.index - i).argsort()[0]].name)
fig.append_trace(plotly.graph_objs.Scatter(x=x,
y=[dict_sex[sex]] * len(x),
text=[kmfs[s].event_table[kmfs[s].event_table.index == t].at_risk.values[0] for t in x],
mode='text',
showlegend=False),
2, 1)

# just a dummy line used as a spacer/header
t = [''] * len(x)
t[1] = 'Patients at risk'
fig.append_trace(plotly.graph_objs.Scatter(x=x,
y=[''] * len(x),
text=t,
mode='text',
showlegend=False),
2, 1)


# prettier layout
x_axis_range = [x_min - x_max * 0.05, x_max * 1.05]
fig['layout']['xaxis2']['visible'] = False
fig['layout']['xaxis2']['range'] = x_axis_range
fig['layout']['xaxis']['range'] = x_axis_range
fig['layout']['yaxis']['domain'] = [0.4, 1]
fig['layout']['yaxis2']['domain'] = [0.0, 0.3]
fig['layout']['yaxis2']['showgrid'] = False
fig['layout']['yaxis']['showgrid'] = False

plotly.offline.iplot(fig)

enter image description here示例2 - 结肠癌的不同治疗

df = pd.read_csv('http://www-eio.upc.edu/~pau/cms/rdata/csv/survival/colon.csv')

fig = plotly.tools.make_subplots(rows=2, cols=1, print_grid=False)
kmfs = []

steps = 5 # the number of time points where number of patients at risk which should be shown

x_min = 0 # min value in x-axis, used to make sure that both plots have the same range
x_max = 0 # max value in x-axis

for rx in df.rx.unique():
T = df[df.rx == rx]["time"]
E = df[df.rx == rx]["status"]
kmf = lifelines.KaplanMeierFitter()

kmf.fit(T, event_observed=E)
kmfs.append(kmf)
x_max = max(x_max, max(kmf.event_table.index))
x_min = min(x_min, min(kmf.event_table.index))
fig.append_trace(plotly.graph_objs.Scatter(x=kmf.survival_function_.index,
y=kmf.survival_function_.values.flatten(),
name=rx),
1, 1)


fig_patients = []
for s, rx in enumerate(df.rx.unique()):
kmf = kmfs[s].event_table
x = []
for i in range(0, int(x_max), int(x_max / (steps - 1))):
x.append(kmf.iloc[np.abs(kmf.index - i).argsort()[0]].name)
fig.append_trace(plotly.graph_objs.Scatter(x=x,
y=[rx] * len(x),
text=[kmfs[s].event_table[kmfs[s].event_table.index == t].at_risk.values[0] for t in x],
mode='text',
showlegend=False),
2, 1)

# just a dummy line used as a spacer/header
t = [''] * len(x)
t[1] = 'Patients at risk'
fig.append_trace(plotly.graph_objs.Scatter(x=x,
y=[''] * len(x),
text=t,
mode='text',
showlegend=False),
2, 1)


# prettier layout
x_axis_range = [x_min - x_max * 0.05, x_max * 1.05]
fig['layout']['xaxis2']['visible'] = False
fig['layout']['xaxis2']['range'] = x_axis_range
fig['layout']['xaxis']['range'] = x_axis_range
fig['layout']['yaxis']['domain'] = [0.4, 1]
fig['layout']['yaxis2']['domain'] = [0.0, 0.3]
fig['layout']['yaxis2']['showgrid'] = False
fig['layout']['yaxis']['showgrid'] = False

plotly.offline.iplot(fig)

enter image description here

关于python - 绘图图表上的辅助/平行 X 轴 (python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54232879/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com