[英]Markers on seaborn line plot in python
新在這里放置超鏈接。 我的 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
在此處訪問整個文件。 我想要的是在基於 SepsisLabel 的 HR 圖上添加一個標記(參見文件)。 例如,在 ICULOS = 249 時,膿毒症 Label 從 0 變為 1。我想在圖表上顯示這一點,膿毒症 label 發生了變化。 我能夠使用以下代碼計算 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
我使用代碼繪制了圖表:
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 。 標記應該只在 position 249 上。 但它也在 position 0 上。為什么會這樣? 有人可以幫我嗎?
謝謝。
在這種情況下,使用markevery
可能會很棘手,因為它在很大程度上取決於每個患者和每個ICULOS
都只有一個條目。
這是另一種方法,使用顯式散布 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()
對於您的新問題,這里是一個如何將“ICULOS”列轉換為 pandas 日期的示例。 該示例使用日期20210101
對應於ICULOS == 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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.