如何在图例中将图例艺术家从点更改为线?

问题描述 投票:0回答:1

我有一个带有回归线的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)
seaborn legend
1个回答
0
投票

而不是将label添加到regplot(似乎将散点图视为其主要成分),可以通过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 = 'lower left', borderpad=.2)
plt.show()
热门问题
推荐问题
最新问题