【问题标题】:Markers on seaborn line plot in pythonpython中seaborn线图上的标记
【发布时间】:2021-06-09 08:34:00
【问题描述】:

这里是新的,所以放置了超链接。我的数据框看起来像这样。

 HR     ICULOS  SepsisLabel PatientID
100.3      1         0          1
117.0      2         0          1
103.9      3         0          1
104.7      4         0          1
102.0      5         0          1
88.1       6         0          1

访问整个文件here。我想要的是在基于 SepsisLabel 的 HR 图上添加一个标记(参见文件)。例如,在 ICULOS = 249 时,脓毒症标签从 0 变为 1。我想在图表上显示此时,脓毒症标签发生了变化。我能够使用此代码计算位置:

mark = dummy.loc[dummy['SepsisLabel'] == 1, 'ICULOS'].iloc[0]
print("The ICULOS where SepsisLabel changes from 0 to 1 is:", mark)
Output: The ICULOS where SepsisLabel changes from 0 to 1 is: 249

我使用代码绘制了图表:

plt.figure(figsize=(15,6))

ax = plt.gca()

ax.set_title("Patient ID = 1")
ax.set_xlabel('ICULOS')
ax.set_ylabel('HR Readings')
sns.lineplot(ax=ax, 
             x="ICULOS", 
             y="HR", 
             data=dummy, 
             marker = '^', 
             markersize=5, 
             markeredgewidth=1, 
             markeredgecolor='black', 
             markevery=mark)

plt.show()

这就是我得到的:Graph。标记应该只在位置 249 上。但它也在位置 0。为什么会这样?有人可以帮帮我吗?

谢谢。

【问题讨论】:

  • 您可以尝试markevery=[mark] 给出要标记的位置列表。 markevery=249 每 249 个位置设置一个标记,从位置 0 开始。
  • 太棒了。是否可以在图表上的标记上方/下方添加数字(标记)?
  • 使用此错误
  • 其他错误,然后是一些拼写错误? ax.text(mark, dummy.loc[mark, 'HR'], str(dummy.loc[mark, 'ICULOS']) + '\n', ha='center', va='center')?你能解决它们吗?
  • 拼写错误。现在它完成了。你能简单解释一下这段代码做了什么吗?

标签: python pandas dataframe matplotlib seaborn


【解决方案1】:

在这种情况下,使用 markevery 可能会很棘手,因为这在很大程度上取决于每个患者和每个 ICULOS 都只有一个条目。

这是另一种方法,使用显式散点图来绘制标记:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.DataFrame({'HR': np.random.randn(200).cumsum() + 60,
                   'ICULOS': np.tile(np.arange(1, 101), 2),
                   'SepsisLabel': np.random.binomial(2, 0.05, 200),
                   'PatientID': np.repeat([1, 2], 100)})
for patient_id in [1, 2]:
    dummy = df[df['PatientID'] == patient_id]
    fig, ax = plt.subplots(figsize=(15, 6))
    ax.set_title(f"Patient ID = {patient_id}")
    ax.set_xlabel('ICULOS')
    ax.set_ylabel('HR Readings')
    sns.lineplot(ax=ax,
                 x="ICULOS",
                 y="HR",
                 data=dummy)
    x = dummy[dummy['SepsisLabel'] == 1]["ICULOS"].values[0]
    y = dummy[dummy['SepsisLabel'] == 1]["HR"].values[0]
    ax.scatter(x=x,
               y=y,
               marker='^',
               s=5,
               linewidth=1,
               edgecolor='black')
    ax.text(x, y, str(x) + '\n', ha='center', va='center', color='red')
    plt.show()

对于您的新问题,以下是如何将“ICULOS”列转换为熊猫日期的示例。该示例使用日期20210101ICULOS == 1 对应。您可能对每位患者都有不同的开始日期。

df_fb = pd.DataFrame()
df_fb['Y'] = df['HR']
df_fb['DS'] = pd.to_datetime('20210101') + pd.to_timedelta(df['ICULOS'] - 1, unit='D')

【讨论】:

  • 解决了我的问题。谢谢 :)
  • 这是一个时间序列数据,但我没有日期信息。我可以将 ICULOS 转换为日期时间格式吗?那么,我可以运行 Fbprophet 算法吗?
  • 使用 datetime 函数,您可以使用 ICULOS 作为 timedelta 添加到开始日期。
  • 怎么样?我试过了:df_fb = df_8[['ICULOS', 'HR']].copy()df_fb.rename(columns = {'ICULOS':'DS'}, inplace = True)df_fb.rename(columns = {'HR':'Y'}, inplace = True)DS Y1 100.3然后我做了df_fb['DS'] = pd.to_datetime(df_fb['DS'])这产生了:0 1970-01-01 00:00:00.000000001 100.3
  • 我更新了答案,举例说明了如何将 ICULOS 转换为日期。
猜你喜欢
  • 2020-03-18
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2019-10-12
  • 2018-07-02
  • 2019-01-31
  • 1970-01-01
相关资源
最近更新 更多