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

标签 python matplotlib data-visualization distribution seaborn

下面是简单的代码:

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/

相关文章:

javascript - HTML5 + JS 极坐标散点图

python - 如何显示和下载位于静态文件夹之外的图像?

python - 如何使用 Python 创建 DAG(用于基于依赖项的任务执行。)

d3.js - d3.js 中的有向无环图

python - 更改基本条形图的宽度

python - Matplotlib 中 X、Y 和 Z 轴的等距缩放?

python ,Seaborn : how to replicate corrplot?

python - Pandas bool 索引 : matching a set

python - BeautifulSoup:6k 条记录 - 但在解析 20 行后停止

python - Folium map 无法在 github 上的笔记本中呈现