python 制作正态分布图,画出拒绝域

下面的程序实现画出正态分布图形,并且画出相应的拒绝域范围。

from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
import scipy.stats as stats
style.use('bmh')
def draw_norm(mean, std):
    x = np.linspace(mean-4*std, mean+4*std, 100)
    y = norm.pdf(x, loc=mean, scale = std)
    # plt.plot(x, y, linewidth = 1)
    plt.plot(x, y )
    plt.text(x = mean, y = 0, s="$ \mu = {} $".format(mean), size =15)
    # plt.text(x = mean, y = 0, s="$ x = \mu $", size = 20)
    # plt.text(x = mean+std, y = np.max(y), s = "$ \sigma  $", size = 20)
    plt.text(x = mean + 2*std, y = np.max(y)*0.7, s = "$ \sigma = {} $".format(np.round(std, decimals=3)), size =15)
    # plt.xticks([])
    # plt.yticks([])
    plt.axvline(x = mean,linewidth = 1, linestyle = '--')
    plt.axhline(y = 0 ,linewidth = 1, linestyle = '--')
    # plt.text(x = mean+std, y = np.median(y), s = "$ ")
    plt.tight_layout()

def draw_area(mean, std, low, high):
    x = np.linspace(mean + low*std, mean + high*std, 100)
    y = stats.norm.pdf(x, loc = mean, scale = std)
    plt.fill_between(x, y, color = "red")

def draw_line(x):
    plt.axvline(x=x, color = "green", linestyle = "--")

def draw_two(mean, se, alpha):
    draw_norm(mean, std=se)
    if alpha == 0.05:
        draw_area(mean=mean, std=se, low=-4, high=-1.96 )
        draw_area(mean, std = se,  low=1.96, high=4)
    elif alpha == 0.1:
        draw_area(mean, std=se, low=-4, high=-1.65)
        draw_area(mean, std = se, low=1.65, high=4)
    else:
        draw_area(mean, std = se, low=-4, high=-2.58)
        draw_area(mean,std =  se, low=2.58, high=4)
        
from numpy import sqrt
draw_norm(69.2, sqrt(49.3)/sqrt(50))
draw_norm(67.5, sqrt(64.5)/sqrt(80))

draw_norm(0, 1)
draw_area(0, 1, 1.65, 4)
draw_line(1.27)

效果如下图
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值