在 Python 中绘制垂直正态分布

Plot a vertical Normal Distribution in Python

这是我当前使用 matplotlib 绘图的代码:

from matplotlib import pyplot
import numpy as np

std=1.5
al=0.6
dpi=80
target=38.9675
mc_min=np.array([10-std, 15-std, 20-std, 25-std, 30-std, 35-std])
mc_max=np.array([2*std, 2*std, 2*std, 2*std, 2*std, 2*std])
mc_min_out=np.array([40-std, 45-std])
mc_max_out=np.array([2*std, 2*std])

x = np.linspace(10, 35, 6)
x_out=np.linspace(40, 45, 2)


a=35+((target-35)*1.5)
b=((target-35)*1.5)

#8,6
pyplot.figure(num=None, figsize=(8, 6), dpi=dpi, facecolor='w', edgecolor='k')

pyplot.bar(x, mc_min, width=3, color ='#000000', align='center', alpha=1) 
pyplot.bar(x_out, mc_min_out, width=3, color ='#000000', align='center', alpha=al/2) 
pyplot.bar(x, mc_max, width=3, bottom=mc_min, color ='#ff0000', align='center', alpha=al)
pyplot.bar(x_out, mc_max_out, width=3, bottom=mc_min_out, color ='#ff0000', align='center', alpha=al/2) 

pyplot.scatter(35, target, s=20, c='y')
pyplot.scatter(35, a, s=20, c='b')
pyplot.scatter(30, a-5, s=20, c='b')
pyplot.scatter(25, a-10, s=20, c='b')
pyplot.scatter(20, a-15, s=20, c='b')
pyplot.scatter(15, a-20, s=20, c='b')
pyplot.scatter(10, a-25, s=20, c='b')




pyplot.axvline(x=35, ymin=0, ymax = 0.9, linewidth=1, color='k')           
pyplot.axvline(x=30, ymin=0, ymax = 0.9, linewidth=1, color='k')           
pyplot.axvline(x=25, ymin=0, ymax = 45, linewidth=1, color='k')           
pyplot.axvline(x=20, ymin=0, ymax = 45, linewidth=1, color='k')           
pyplot.axvline(x=15, ymin=0, ymax = 45, linewidth=1, color='k')
pyplot.axvline(x=10, ymin=0, ymax = 45, linewidth=1, color='k') 



pyplot.axhline(y=10, xmin=0.04, xmax=0.12, linewidth=1, color='k')    
pyplot.axhline(y=15, xmin=0.16, xmax=0.242, linewidth=1, color='k')           
pyplot.axhline(y=20, xmin=0.278, xmax=0.36, linewidth=1, color='k')           
pyplot.axhline(y=25, xmin=0.4, xmax=0.48, linewidth=1, color='k') 
pyplot.axhline(y=30, xmin=0.515, xmax=0.6, linewidth=1, color='k')                     
pyplot.axhline(y=35, xmin=0.64, xmax=0.72, linewidth=1, color='k')           
pyplot.axhline(y=target, xmin=0.67, xmax=0.69, linewidth=1, color='k')           
pyplot.axhline(y=(a+b), xmin=0.66, xmax=0.70, linewidth=1, color='k')           
pyplot.axhline(y=(a-5+b), xmin=0.54, xmax=0.58, linewidth=1, color='k')           
pyplot.axhline(y=(a-10+b), xmin=0.42, xmax=0.46, linewidth=1, color='k')           
pyplot.axhline(y=(a-15+b), xmin=0.3, xmax=0.34, linewidth=1, color='k')           
pyplot.axhline(y=(a-20+b), xmin=0.18, xmax=0.22, linewidth=1, color='k')           
pyplot.axhline(y=(a-25+b), xmin=0.06, xmax=0.10, linewidth=1, color='k')


pyplot.yticks(np.arange(0, 56, 5))          

这是结果:

我的问题是我想在穿过 35 x-positioned 柱的垂直线上绘制正态分布。正态分布的均值等于变量 "a",标准差值为 "b",并且位于红色条的边缘 (35 x-positioned) 和顶部水平线之间穿过垂直 35 x-positioned 线。结果将是第二张照片。

您可以通过向绘制的数据添加 x 和 y 偏移量,在您想要的位置绘制高斯函数。这是一个示例函数:

def draw_gaussian_at(support, sd=1.0, height=1.0, 
        xpos=0.0, ypos=0.0, ax=None, **kwargs):
    if ax is None:
        ax = plt.gca()
    gaussian = np.exp((-support ** 2.0) / (2 * sd ** 2.0))
    gaussian /= gaussian.max()
    gaussian *= height
    return ax.plot(gaussian + xpos, support + ypos, **kwargs)

xposypos将曲线的中心指向该位置,sdheight控制曲线的形状。使用负值 height 使曲线 "face" 向左。 support 参数是曲线运行的 y 值 的范围,因此在您的情况下,它类似于 np.linspace(a - 3.0 * b, a + 3.0 * b, 1000),它将绘制曲线以 a.

为中心的超过 3 个标准偏差

这是函数用法的示例:

support = np.linspace(-2, 2, 1000)
fig, ax = plt.subplots()
for each in np.linspace(-2, 2, 5):
    draw_gaussian_at(support, sd=0.5, height=-0.5, xpos=each, ypos=each, ax=ax, color='k')