在Seaborn中,我已经相互绘制了两个变量,并使用hue
关键字将变量分为两类。
我想用确定系数注释每条回归线。 This question仅描述如何使用图例显示行的标签。
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"})
答案 0 :(得分:1)
按原样使用代码:
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)