本文介绍了如何在 seaborn.distplot 中填充不同颜色的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用一种颜色填充两条阈值线(line1 和 line2)之外的区域,并由 distplot 绘制的 KDE 曲线限制在 Y 轴上?(代表我的应用程序的 3-sigma)

导入pylab为pl将 seaborn 作为 sns 导入#绘制两条线p1 = pl.axvline(x=line1,color='#EF9A9A')p2 = pl.axvline(x=line2,color='#EF9A9A')#绘制PDFsns.distplot(stat, hist=True,color='#388E3C')
解决方案

您可以使用 fill_between 来填充曲线下方的区域.要从 seaborn 图中访问 KDE 曲线,您可以先绘制该曲线,这样 ax.lines 只有一个元素,即感兴趣的曲线.它的数据通过kde_x, kde_y = ax.lines[0].get_data()获得.

然后使用 ax.fill_between() 允许填充曲线下方的区域.为了将其限制在某个给定的数据范围之外,可以使用 where 关键字参数(并且应该设置 interpolate=True 以使该区域上升到问题).

ax.fill_between(kde_x, kde_y, where=(kde_xx1) ,插值=真,颜色=#EF9A9A")

完整示例:

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt将 seaborn 作为 sns 导入stat=np.random.randn(100)x0 = -1x1 = 1#plotting PDF(在绘制其他任何内容之前执行此操作)ax = sns.distplot(stat, hist=True,color='#388E3C')kde_x, kde_y = ax.lines[0].get_data()#绘制两条线p1 = plt.axvline(x=x0,color='#EF9A9A')p2 = plt.axvline(x=x1,color='#EF9A9A')ax.fill_between(kde_x, kde_y, where=(kde_xx1) ,插值=真,颜色=#EF9A9A")plt.show()

最初问题的旧答案:

您可以使用

这里,我们需要在设置spans后调整xlimits;原因是跨距就位后,自动缩放会在轴的两端再增加 5% 的填充,从而产生空白.或者,您可以对 xaxis 使用零边距,ax.margins(x=0).

Is it possible to fill with a color the area outside the two threshold lines (line1 and line2) and limited in Y-axis by the KDE curve drawn by distplot ?(that represents 3-sigmas for my application)

import pylab as pl
import seaborn as sns
#plotting the two lines
p1 = pl.axvline(x=line1,color='#EF9A9A')
p2 = pl.axvline(x=line2,color='#EF9A9A')
#plotting the PDF
sns.distplot(stat, hist=True,color='#388E3C')
解决方案

You may use fill_between to fill the area underneath a curve. To get access to the KDE curve from the seaborn plot, you can draw that one first, such that ax.lines only has a single element, which is the curve of interest. Its data is obtained via kde_x, kde_y = ax.lines[0].get_data().

Then using ax.fill_between() allows to fill the area under the curve. To restrict this to be outside some given data range, the where keyword argument may be used (and interpolate=True should be set to have the area go up to the points in question).

ax.fill_between(kde_x, kde_y, where=(kde_x<x0) | (kde_x>x1) ,
                interpolate=True, color='#EF9A9A')

Full example:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

stat=np.random.randn(100)
x0 = -1
x1 = 1

#plotting the PDF (do this before plotting anything else)
ax = sns.distplot(stat, hist=True,color='#388E3C')
kde_x, kde_y = ax.lines[0].get_data()

#plotting the two lines
p1 = plt.axvline(x=x0,color='#EF9A9A')
p2 = plt.axvline(x=x1,color='#EF9A9A')


ax.fill_between(kde_x, kde_y, where=(kde_x<x0) | (kde_x>x1) ,
                interpolate=True, color='#EF9A9A')

plt.show()


Old answer to initial question:

You may use an axvspan, starting at the left x limit and going to the position of the first line and another one starting at the position of the second line and going to the right x limit.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

stat=np.random.randn(100)
x0 = -1
x1 = 1

#plotting the two lines
p1 = plt.axvline(x=x0,color='#EF9A9A')
p2 = plt.axvline(x=x1,color='#EF9A9A')

#plotting the PDF
ax = sns.distplot(stat, hist=True,color='#388E3C')

xlim = ax.get_xlim()
ax.axvspan(xlim[0], x0, color='#EF9A9A', alpha=0.5)
ax.axvspan(x1, xlim[1], color='#EF9A9A', alpha=0.5)
#reset xlim
ax.set_xlim(xlim)
plt.show()

Here, we need to adjust the xlimits after setting spans; the reason is that with the spans in place the autoscaling would add another 5% padding to both ends of the axes, resulting in white space. Alternatively you could use zero margin for the xaxis, ax.margins(x=0).

这篇关于如何在 seaborn.distplot 中填充不同颜色的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:47