我一直在尝试将图例添加到下面的代码中。当我添加“标签”时,它应该可以工作。但它就是不显示,不确定我做错了什么。
使用的包
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');
发布于 2021-10-23 05:24:22
您只需调用Axes对象的.legend()
方法即可。seaborn的绘图函数直接返回对轴的引用,这很方便。请参阅sns.kdeplot
的文档
ax = sns.kdeplot(...)
ax.legend(loc="upper right")
https://stackoverflow.com/questions/69688499
复制相似问题