在Python中绘制垂直正态分布

时间:2017-12-01 16:03:48

标签: python matplotlib plot distribution gaussian

这是我目前使用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))          

这就是结果:

Resulting Plot

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

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以通过将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的负值使曲线“面向”左侧。 support参数是曲线运行的 y-values 的范围,因此在您的情况下,它将类似于np.linspace(a - 3.0 * b, a + 3.0 * b, 1000),它将绘制曲线超过3个标准偏差集中在a

以下是该函数用法的示例:

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')

Gaussians at different positions