在python中使用fill_between阴影密度曲线的子区域

时间:2018-07-20 12:14:41

标签: python matplotlib

我想在正态分布的子部分(例如左侧的5%瓷砖)之间填充。

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之间的区域

px=np.arange(0,500,10)
plt_fill_between(px,iq.pdf(px),color='r')

问题在于,上面的pdf仅以红色显示0到500之间的pdf。 我想显示从0到2000的完整pdf,其中0到500带有阴影? 知道如何创建它吗?

2 个答案:

答案 0 :(得分:1)

如前所述,您需要使用plt.fill_between而不是plt_fill_between。这样做时,输出看起来像这样,似乎正是您要寻找的。

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()

enter image description here

答案 1 :(得分:0)

如果要转到2000,则只能在np.arange中使用从0到500的x值:

px=np.arange(0,2000,10)
plt.fill_between(px,iq.pdf(px),color='r')