gpt4 book ai didi

python - 使用 Seaborn 的分布图的部分阴影

转载 作者:行者123 更新时间:2023-11-28 20:14:33 24 4
gpt4 key购买 nike

下面是简单的代码:

import numpy as np
import seaborn as sns
dist = np.random.normal(loc=0, scale=1, size=1000)
ax = sns.kdeplot(dist, shade=True);

产生下图:

enter image description here

我只想把所有东西都涂上阴影(或留下一些 x 值)。最简单的方法是什么?我准备使用 Seaborn 以外的东西。

最佳答案

调用ax = sns.kdeplot(dist, shade=True)后,ax.get_lines()中最后一行对应kde密度曲线:

ax = sns.kdeplot(dist, shade=True)
line = ax.get_lines()[-1]

您可以使用line.get_data 提取与该曲线对应的数据:

x, y = line.get_data()

获得数据后,例如,您可以通过选择这些点并调用 ax.fill_between 来为对应于 x > 0 的区域着色:

mask = x > 0
x, y = x[mask], y[mask]
ax.fill_between(x, y1=y, alpha=0.5, facecolor='red')

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
dist = np.random.normal(loc=0, scale=1, size=1000)
ax = sns.kdeplot(dist, shade=True)
line = ax.get_lines()[-1]
x, y = line.get_data()
mask = x > 0
x, y = x[mask], y[mask]
ax.fill_between(x, y1=y, alpha=0.5, facecolor='red')
plt.show()

enter image description here

关于python - 使用 Seaborn 的分布图的部分阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099765/

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