This is my current code of my plotting with 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. 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. 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. 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. Use a negative value for height
to have the curve "face" to the left. 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
.
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.