gpt4 book ai didi

python - 如何修改 sns.histplot 中的核密度估计线

转载 作者:行者123 更新时间:2023-12-05 00:52:42 25 4
gpt4 key购买 nike

我正在创建一个直方图(频率与计数),我想添加不同颜色的核密度估计线。我怎样才能做到这一点?例如我想改变颜色

sns.histplot(data=penguins, x="flipper_length_mm", kde=True)

enter image description here

示例取自 https://seaborn.pydata.org/generated/seaborn.histplot.html

最佳答案

histplotline_kws={...} 旨在改变 kde 行的外观。但是,当前的 seaborn 版本不允许以这种方式更改颜色,可能是因为颜色与 hue 参数一起使用(尽管在这种情况下不使用 hue )。

import seaborn as sns

penguins = sns.load_dataset('penguins')
ax = sns.histplot(data=penguins, x="flipper_length_mm", kde=True,
line_kws={'color': 'crimson', 'lw': 5, 'ls': ':'})

histplot changing kde line parameters

seaborn's github ,建议分别绘制histplotkdeplot。为了使两者在 y 方向上匹配,有必要将 histplotstat='density' 一起使用(kdeplot 没有使用 histplot 的默认 stat='count' 的参数)。

penguins = sns.load_dataset('penguins')
ax = sns.histplot(data=penguins, x="flipper_length_mm", kde=False, stat='density')
sns.kdeplot(data=penguins, x="flipper_length_mm", color='crimson', ax=ax)

sns.histplot and sns.kdeplot separately

如果确实需要 count 统计信息,另一种方法是通过 matplotlib 更改线条颜色:

penguins = sns.load_dataset('penguins')
ax = sns.histplot(data=penguins, x="flipper_length_mm", kde=True)
ax.lines[0].set_color('crimson')

changing line color of sns.histplot with kde=True

关于python - 如何修改 sns.histplot 中的核密度估计线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69524514/

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