简体   繁体   English

python 中 seaborn 行 plot 上的标记

[英]Markers on seaborn line plot in python

New here so putting hyperlinks.新在这里放置超链接。 My dataframe looks like this.我的 dataframe 看起来像这样。

 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

Access the whole file here .此处访问整个文件。 What I wanted is to add a marker on the HR graph based on SepsisLabel (See the file).我想要的是在基于 SepsisLabel 的 HR 图上添加一个标记(参见文件)。 Eg, at ICULOS = 249, Sepsis Label changed from 0 to 1. I wanted to show that at this point on graph, sepsis label changed.例如,在 ICULOS = 249 时,脓毒症 Label 从 0 变为 1。我想在图表上显示这一点,脓毒症 label 发生了变化。 I was able to calculate the position using this code:我能够使用以下代码计算 position:

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

I Plotted the graph using the code:我使用代码绘制了图表:

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()

This is what I got: Graph .这就是我得到的: Graph The marker was supposed to be on position 249 only.标记应该只在 position 249 上。 But it is also on position 0. Why is it happening?但它也在 position 0 上。为什么会这样? Can someone help me out?有人可以帮我吗?

Thanks.谢谢。

Working with markevery can be tricky in this case, as it strongly depends on there being exactly one entry for each patient and each ICULOS .在这种情况下,使用markevery可能会很棘手,因为它在很大程度上取决于每个患者和每个ICULOS都只有一个条目。

Here is an alternative approach, using an explicit scatter plot to draw the marker:这是另一种方法,使用显式散布 plot 来绘制标记:

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()

在 sns.lineplot 上标记一个点

For your new question, here is an example how to convert the 'ICULOS' column to pandas dates.对于您的新问题,这里是一个如何将“ICULOS”列转换为 pandas 日期的示例。 The example uses date 20210101 to correspond with ICULOS == 1 .该示例使用日期20210101对应于ICULOS == 1 You probably have a different starting date for each patient.您可能对每位患者都有不同的开始日期。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM