简体   繁体   English

如何在seaborn.distplot中填充不同颜色的区域

[英]How to fill with a different color an area in seaborn.distplot

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) 是否可以用两个阈值线(line1和line2)之外的区域填充颜色,并通过distplot绘制的KDE曲线限制在Y轴?(表示我的应用程序的3-sigmas)

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. 您可以使用fill_between来填充曲线下方的区域。 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. 要从seaborn图中访问KDE曲线,您可以先绘制一条曲线,这样ax.lines只有一个元素,这是感兴趣的曲线。 Its data is obtained via kde_x, kde_y = ax.lines[0].get_data() . 其数据通过kde_x, kde_y = ax.lines[0].get_data()

Then using ax.fill_between() allows to fill the area under the curve. 然后使用ax.fill_between()允许填充曲线下的区域。 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). 要将此限制在某个给定的数据范围之外,可以使用where关键字参数(并且应设置interpolate=True以使该区域上升到所讨论的点)。

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. 您可以使用axvspan ,从左边的x限制开始,到第一行的位置,另一个从第二行的位置开始,然后到右边的x限制。

 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; 在这里,我们需要在设置跨度后调整xlimits; 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. 原因是,在跨度到位的情况下,自动缩放会在轴的两端添加另外5%的填充,从而产生空白区域。 Alternatively you could use zero margin for the xaxis, ax.margins(x=0) . 或者,您可以对xaxis使用零边距, ax.margins(x=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM