你如何在 Seaborn 中为 kde 情节创造传奇?

Posted

技术标签:

【中文标题】你如何在 Seaborn 中为 kde 情节创造传奇?【英文标题】:How do you create a legend for kde plot in Seaborn? 【发布时间】:2021-01-08 21:00:12 【问题描述】:

我有一个 kdeplot,但我正在努力弄清楚如何创建图例。

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)

得到我这个:

我试图得到这个:

【问题讨论】:

【参考方案1】:

一种方法是将label= 传递给kdeplot,然后请求显示图例。

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()

另一种方法是使用 seaborn 必须基于 hue= 列拆分数据帧的内置方式。在你的情况下,它看起来像下面,但不知道你的数据框的结构,不可能确定。请参阅the documentation 了解更多信息。

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

【讨论】:

【参考方案2】:

当每个 kdeplot 使用一种颜色时,另一个答案效果很好。

如果使用诸如“红色”之类的颜色图,则会显示非常浅的红色。自定义颜色图可以显示范围中间的颜色:

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()

【讨论】:

以上是关于你如何在 Seaborn 中为 kde 情节创造传奇?的主要内容,如果未能解决你的问题,请参考以下文章

Seaborn 概率直方图 - KDE 归一化

将 seaborn 情节图例移动到不同的位置

如何更好地适应 seaborn 小提琴情节?

如何缩放具有不同频率的多个KDE图?

在 seaborn distplot KDE 估计中限制 x 的范围

AttributeError:“模块”对象没有属性“情节”-Seaborn