首页 > 解决方案 > 如何在 regplot 中将传奇艺术家从点更改为线?

问题描述

我有一个带有回归线的 regplot。当我添加图例时,图例的标签是一个点。如何更改为点到线?

ax = sns.regplot(x='chronolgical age', y = i_feature,
                data = plot_datai[plot_datai['Sex']==-1], fit_reg=True, 
                scatter_kws={'alpha':0.8, 's':2}, line_kws={'alpha':0.8, 'linewidth':2},
                color = sex_color[0], label = 'Female',
                x_jitter = .2, order = 2)

ax = sns.regplot(x='chronolgical age', y = i_feature,
                 data = plot_datai[plot_datai['Sex']==1], fit_reg=True, 
                 scatter_kws={'alpha':0.8, 's':2}, line_kws={'alpha':0.8, 'linewidth':2},
                 color = sex_color[1], label = 'Male',
                 x_jitter = .2, order = 2)

ax.legend(loc = 'lower left', borderpad=.2)

标签: seabornlegend

解决方案


可以通过 将 设置为线图,而不是添加labelregplot(这似乎将散点图视为其主要组成部分)。labelline_kws

以下是一些使用“提示”数据集以及问题中使用的选项的示例代码:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
ax = sns.regplot(x="total_bill", y="tip", data=tips[tips["sex"] == "Female"],
                 fit_reg=True,
                 scatter_kws={'alpha': 0.8, 's': 2},
                 line_kws={'alpha': 0.8, 'linewidth': 2, 'label': 'Female'},
                 color='crimson',  # label='Female',
                 x_jitter=.2, order=2)
ax = sns.regplot(x="total_bill", y="tip", data=tips[tips["sex"] == "Male"],
                 fit_reg=True,
                 scatter_kws={'alpha': 0.8, 's': 2},
                 line_kws={'alpha': 0.8, 'linewidth': 2, 'label': 'Male'},
                 color='dodgerblue',  # label='Male',
                 x_jitter=.2, order=2)
ax.legend(loc='upper left', borderpad=.2)
plt.show()

演示图


推荐阅读