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

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)

效果如下图

python画三维正态分布曲线 python正态分布画图_python画三维正态分布曲线