python 给曲线下方指定区域涂色
主要用到plt.fill_between()方法,重要参数:x:横轴区域y:纵轴区域color:颜色alpha:透明度,1则完全不透明def getprobs(x, sigma, mu):return 1/(sqrt(2*pi)*sigma)*exp(-(x-mu)**2/(2*sigma**2))x = linspace(-8, 8, 100)sigma = 2mu...
·
主要用到plt.fill_between()方法,重要参数:
- x:横轴区域
- y:纵轴区域
- color:颜色
- alpha:透明度,1则完全不透明
def getprobs(x, sigma, mu):
return 1/(sqrt(2*pi)*sigma)*exp(-(x-mu)**2/(2*sigma**2))
x = linspace(-8, 8, 100)
sigma = 2
mu = 0
y = getprobs(x, sigma, mu)
plt.plot(x, y, 'b')
low = mu-2*sigma
high = mu+2*sigma
# (mu-2sigma,mu+2sigma)
normal = linspace(low,high,20)
normal_y = getprobs(normal,sigma,mu)
plt.fill_between(normal, normal_y, color='g', alpha=0.7)
# (-8,mu-2sigma)
abnormal_low = linspace(-8,low,5)
abnormal_low_y = getprobs(abnormal_low,sigma,mu)
plt.fill_between(abnormal_low, abnormal_low_y, color='r', alpha=0.7)
# (mu+2sigma,8)
abnormal_high = linspace(high,8,5)
abnormal_high_y = getprobs(abnormal_high,sigma,mu)
plt.fill_between(abnormal_high, abnormal_high_y, color='r', alpha=0.7)
plt.xticks([]) # 不显示x轴y轴
plt.yticks([])
plt.show()
更多推荐
已为社区贡献4条内容
所有评论(0)