使用Seaborn的分布图的局部阴影 [英] Partial shade of distribution plot using 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);
产生以下图像:
< a href = https://i.stack.imgur.com/b8kYj.png rel = nofollow noreferrer>
我只想对所有内容进行阴影处理(或保留一些x值)。最简单的方法是什么?我已经准备好使用Seaborn以外的其他东西。
I would like to only shade everything right (or left to some x value). What is the simplest way? I am ready to use something other than Seaborn.
推荐答案
调用 ax = sns.kdeplot(dist ,shade = True)
, ax.get_lines()
的最后一行对应于kde密度曲线:
After calling ax = sns.kdeplot(dist, shade=True)
, the last line in ax.get_lines()
corresponds to the kde density curve:
ax = sns.kdeplot(dist, shade=True)
line = ax.get_lines()[-1]
您可以使用 line.get_data
提取与该曲线相对应的数据:
You can extract the data corresponding to that curve using line.get_data
:
x, y = line.get_data()
一旦有了数据,就可以例如将与 x> 0
,选择这些点并调用 ax.fill_between
:
Once you have the data, you can, for instance, shade the region corresponding to x > 0
by selecting those points and calling ax.fill_between
:
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()
这篇关于使用Seaborn的分布图的局部阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!