简体   繁体   中英

How do you create a legend for kde plot in Seaborn?

I have a kdeplot but I'm struggling to figure out how to create the legend.

import matplotlib.patches as mpatches  # see the tutorial for how we use mpatches to generate this figure!

# Set 'is_workingday' to a boolean array that is true for all working_days
is_workingday = daily_counts["workingday"] == "yes"
is_not_workingday = daily_counts['workingday'] == "no"
# Bivariate KDEs require two data inputs. 
# In this case, we will need the daily counts for casual and registered riders on workdays
casual_workday = daily_counts.loc[is_workingday, 'casual']
registered_workday = daily_counts.loc[is_workingday, 'registered']

# Use sns.kdeplot on the two variables above to plot the bivariate KDE for weekday rides
sns.kdeplot(casual_workday, registered_workday, color = "red", cmap = "Reds", hue = "workingday", legend = True)

# Repeat the same steps above but for rows corresponding to non-workingdays
casual_non_workday =  daily_counts.loc[is_not_workingday, 'casual']
registered_non_workday = daily_counts.loc[is_not_workingday, 'registered']


# Use sns.kdeplot on the two variables above to plot the bivariate KDE for non-workingday rides
sns.kdeplot(casual_non_workday, registered_non_workday, color = 'blue', cmap = "Blues", legend = True, shade = False)

Gets me this: 在此处输入图像描述

I'm trying to get this: 在此处输入图像描述

One way is to pass a label= to kdeplot and then request the legend to be shown.

geyser = sns.load_dataset("geyser")
long = geyser.loc[geyser['kind']=='long']
short = geyser.loc[geyser['kind']=='short']
sns.kdeplot(x=long["waiting"], y=long["duration"], label='long')
sns.kdeplot(x=short["waiting"], y=short["duration"], label='short')
plt.legend()

在此处输入图像描述

An other way is to use the built-in way that seaborn has to split a dataframe based on a hue= column. In you case, it would look something like below, but not knowing the structure of your dataframe, it's impossible to know for sure. See the documentation for more information.

sns.kdeplot(x='casual', y='registered', hue='workingday', data=daily_counts, shade=False, legend=True)

The other answer works nice when one single color is used per kdeplot.

In case a colormap such as 'Reds' is used, this would show a very light red. A custom colormap can show a color from the middle of the range:

from matplotlib import pyplot as plt
import matplotlib.patches as  mpatches
import seaborn as sns
import numpy as np

casual_workday = np.random.randn(100) * 1.2
registered_workday = 0.8 * np.random.randn(100) + casual_workday * 0.2 + 1
sns.kdeplot(x=casual_workday, y=registered_workday, color="red", cmap="Reds", shade=False)

casual_non_workday = np.random.randn(100) * 1.6
registered_non_workday = 0.5 * np.random.randn(100) + casual_non_workday * 0.5 - 1
sns.kdeplot(x=casual_non_workday, y=registered_non_workday, cmap="Blues", shade=False)

handles = [mpatches.Patch(facecolor=plt.cm.Reds(100), label="Workday"),
           mpatches.Patch(facecolor=plt.cm.Blues(100), label="Non-workday")]
plt.legend(handles=handles)
plt.show()

示例图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM