【问题标题】:How to display Legend with seaborn.kdeplot如何使用 seaborn.kdeplot 显示图例
【发布时间】:2021-12-09 18:49:13
【问题描述】:

我一直在尝试将图例添加到下面的代码中。 当我添加“标签”时,它应该可以工作。但它只是不会显示,不知道我做错了什么。

使用的包

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as stats
from scipy.stats import kruskal
from sklearn.datasets import load_iris

Df1 = pd.read_csv(r"C:\Users\pc admin\Desktop\SUTD Programming\Data Wrangling\Personal Assigment\IBM Data.csv", header=0)


plt.figure(figsize=(20,8))
plt.style.use('seaborn-colorblind')
plt.grid(True, alpha=0.5)
sns.kdeplot(Df1.loc[Df1['Attrition'] == 'No', 'JobSatisfaction'], **label = "Previous-Employee"**)
sns.kdeplot(Df1.loc[Df1['Attrition'] == 'Yes', 'JobSatisfaction'], **label ="Current-Employees"**)
plt.xlabel('JobSatisfaction')
plt.xlim(left=0)
plt.ylabel('Density')
plt.title('Distance From Home Distribution in Percent by Attrition Status');

【问题讨论】:

    标签: python matplotlib seaborn legend


    【解决方案1】:

    您只需调用 Axes 对象的 .legend() 方法。 seaborn 的绘图函数直接返回对轴的引用,这很方便。见sns.kdeplot的文档

    ax = sns.kdeplot(...)
    ax.legend(loc="upper right")
    

    【讨论】:

      猜你喜欢
      • 2016-11-22
      • 2019-04-12
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2013-03-03
      • 2017-01-30
      相关资源
      最近更新 更多