簡體   English   中英

如何在seaborn lmplot中注釋回歸線?

[英]How to annotate regression lines in seaborn lmplot?

我已經在Seaborn作圖彼此兩個變量和所使用的hue關鍵字變量分成兩大類。

我想用決定系數來注釋每條回歸線。 此問題僅描述如何使用圖例顯示線條的標簽。

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

df = pd.read_excel(open('intubation data.xlsx', 'rb'), sheet_name='Data 
(pretest)', header=1, na_values='x')
vars_of_interest = ['PGY','Time (sec)','Aspirate (cc)']
df['Resident'] = df['PGY'] < 4

 lm = sns.lmplot(x=vars_of_interest[1], y=vars_of_interest[2],
        data=df, hue='Resident', robust=True, truncate=True,
        line_kws={'label':"bob"})

按原樣使用您的代碼:

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

df = pd.read_excel(open('intubation data.xlsx', 'rb'), sheet_name='Data 
(pretest)', header=1, na_values='x')
vars_of_interest = ['PGY','Time (sec)','Aspirate (cc)']
df['Resident'] = df['PGY'] < 4

p = sns.lmplot(x=vars_of_interest[1], y=vars_of_interest[2],
        data=df, hue='Resident', robust=True, truncate=True,
        line_kws={'label':"bob"}, legend=True)
# assuming you have 2 groups
ax = p.axes[0, 0]
ax.legend()
leg = ax.get_legend()
L_labels = leg.get_texts()
# assuming you computed r_squared which is the coefficient of determination somewhere else
label_line_1 = r'$R^2:{0:.2f}$'.format(0.3)
label_line_2 = r'$R^2:{0:.2f}$'.format(0.21)
L_labels[0].set_text(label_line_1)
L_labels[1].set_text(label_line_2)

瞧:使用我自己的隨機數據創建的圖表,因為 OP 沒有提供任何數據。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM