gpt4 book ai didi

python - 你如何在 Seaborn 中为 kde plot 创建一个传奇?

转载 作者:行者123 更新时间:2023-12-04 14:31:55 24 4
gpt4 key购买 nike

我有一个 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)
给我这个:
enter image description here
我试图得到这个:
enter image description here

最佳答案

一种方法是通过 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()
enter image description here
另一种方法是使用 seaborn 必须基于 hue= 拆分数据帧的内置方式。柱子。在你的情况下,它看起来像下面这样,但不知道你的数据帧的结构,就不可能确定。见 the documentation想要查询更多的信息。
sns.kdeplot(x='casual', y='registered', hue='workingday', data=daily_counts, shade=False, legend=True)

关于python - 你如何在 Seaborn 中为 kde plot 创建一个传奇?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64014746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com