主要用到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()

在这里插入图片描述

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐