在python中使用fill_between阴影密度曲线的子区域 [英] use fill_between in python to shade a sub area of a density curve
问题描述
我想要 Fill_Between 正态分布的一个子部分,比如左边的 5%tile.
I would like to Fill_Between a sub section of a normal distribution, say the left 5%tile.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats as stats
plt.style.use('ggplot')
mean=1000
std=250
x=np.linspace(mean-3*std, mean+3*std,1000)
iq=stats.norm(mean,std)
plt.plot(x,iq.pdf(x),'b')
到目前为止很好.
然后我设置 px 来填充 x=0 到 500 之间的区域
Then I set px to fill the area between x=0 to 500
px=np.arange(0,500,10)
plt_fill_between(px,iq.pdf(px),color='r')
问题是上面只会以红色显示从0到500的pdf.我想显示从0到2000的完整pdf,其中0到500带有阴影?知道如何创建它吗?
The problem is that the above will only show the pdf from 0 to 500 in red. I want to show the full pdf from 0 to 2000 where the 0 to 500 is shaded? Any idea how to create this?
推荐答案
如前所述,您需要使用 plt.fill_between
而不是 plt_fill_between
.这样做时,输出看起来像这样,这似乎正是您要查找的内容.
As commented, you need to use plt.fill_between
instead of plt_fill_between
. When doing so the output looks like this which seems to be exactly what you're looking for.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats as stats
plt.style.use('ggplot')
mean=1000
std=250
x=np.linspace(mean-3*std, mean+3*std,1000)
iq=stats.norm(mean,std)
plt.plot(x,iq.pdf(x),'b')
px=np.arange(0,500,10)
plt.fill_between(px,iq.pdf(px),color='r')
plt.show()
这篇关于在python中使用fill_between阴影密度曲线的子区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!