[英]Plot a vertical Normal Distribution in Python
This is my current code of my plotting with matplotlib: 这是我使用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))
And this is the result: 结果如下:
My problem is that I want to plot a normal distribution in the vertical line that crosses the 35 x-positioned bar. 我的问题是我想在垂直于35 x位置条的垂直线上绘制正态分布。 The normal distribution would have a mean equal to the variable "a" and a standard deviation of value "b" and will fit between the edge of the red bar (35 x-positioned) and the top horizontal line that crosses the vertical 35 x-positioned line. 正态分布的平均值等于变量“ a”,标准偏差为值“ b”,正态分布将位于红色条的边缘(位于35 x位置)和与垂直线35 x交叉的顶部水平线之间定位线。 The result would be as the second photo. 结果将是第二张照片。
You can plot a Gaussian function in the position you want by adding x- and y-offsets to the plotted data. 通过将x和y偏移量添加到绘制的数据,可以在所需位置绘制高斯函数。 Here's an example function: 这是一个示例函数:
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)
xpos
and ypos
direct the center of the curve to that location, and sd
and height
control the shape of the curve. xpos
和ypos
将曲线的中心指向该位置,而sd
和height
控制曲线的形状。 Use a negative value for height
to have the curve "face" to the left. 对height
使用负值可使曲线“面”在左侧。 The support
parameter is the range of y-values over which the curve runs, so in your case it would be something like np.linspace(a - 3.0 * b, a + 3.0 * b, 1000)
, which would plot the curve over 3 standard deviations centered at a
. support
参数是曲线运行的y值范围,因此在您的情况下,它将类似于np.linspace(a - 3.0 * b, a + 3.0 * b, 1000)
,将曲线绘制在3个标准差以a
为中心。
Here's an example of the function's usage: 这是该函数用法的一个示例:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.