How do you plot a gaussian function in python?
Show In the previous post, we calculated the area under the standard normal curve using Python and the Calculating the probability under a normal curve is useful for engineers. This type of calculation can be helpful to predict the likely hood of a part coming off an assembly line being within a given specification when the statistical properties of all the parts that have come of the assembly line previously are known. In this post, we will calculate the probability under the normal curve to answer a question like the one below: GIVEN:At a facility that manufactures electrical resistors, a statistical sample of 1-kΩ resistors is pulled from the production line. The resistor's resistances are measured and recorded. A mean resistance of 979.8 kΩ and a standard deviation of 73.10 kΩ represents the sample of resistors. The desired resistance tolerance for the 1-kΩ resistors is ± 10%. This tolerance range means the acceptable range of resistance is 900 Ω to 1100 Ω. FIND:Assuming a normal distribution, determine the probability that a resistor coming off the production line will be within spec (in the range of 900 Ω to 1100 Ω). Show the probability that a resistor picked off the production line is within spec on a plot. SOLUTION:To build the plot, we will use Python and a plotting package called Matplotlib. We will also use the $ pip install matplotlib $ pip install scipy Before we build the plot, let's take a look at a gaussin curve. The shape of a gaussin curve is sometimes referred to as a "bell curve." This is the type of curve we are going to plot with Matplotlib. Create a new Python script called # normal_curve.py import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm # if using a Jupyter notebook, inlcude: %matplotlib inline Next, we need to define the constants given in the problem. The mean is
# define constants mu = 998.8 sigma = 73.10 x1 = 900 x2 = 1100 Next, we calculate the Z-transform of the lower and upper bound using the mean and standard deviation defined above. # calculate the z-transform z1 = ( x1 - mu ) / sigma z2 = ( x2 - mu ) / sigma After the Z-transform of the lower and upper bounds are calculated, we calculate the probability with SciPy's x = np.arange(z1, z2, 0.001) # range of x in spec x_all = np.arange(-10, 10, 0.001) # entire range of x, both in and out of spec # mean = 0, stddev = 1, since Z-transform was calculated y = norm.pdf(x,0,1) y2 = norm.pdf(x_all,0,1) Finally, we build the plot. Note how Matplotlib's # build the plot fig, ax = plt.subplots(figsize=(9,6)) plt.style.use('fivethirtyeight') ax.plot(x_all,y2) ax.fill_between(x,y,0, alpha=0.3, color='b') ax.fill_between(x_all,y2,0, alpha=0.1) ax.set_xlim([-4,4]) ax.set_xlabel('# of Standard Deviations Outside the Mean') ax.set_yticklabels([]) ax.set_title('Normal Gaussian Curve') plt.savefig('normal_curve.png', dpi=72, bbox_inches='tight') plt.show() The finished plot is below. Notice how the area corresponding to resistors in the given specification (between the upper and lower bounds) is shaded. What is a Gaussian plot?When we plot a dataset such as a histogram, the shape of that charted plot is what we call its distribution. The most commonly observed shape of continuous values is the bell curve, which is also called the Gaussian or normal distribution. It is named after the German mathematician, Carl Friedrich Gauss.
How do you fit a Gaussian curve in Python?It uses non-linear least squares to fit data to a functional form. You can learn more about curve_fit by using the help function within the Jupyter notebook or scipy online documentation. The curve_fit function has three required inputs: the function you want to fit, the x-data, and the y-data you fit.
What is the graph of the Gaussian distribution?The normal distribution is often called the bell curve because the graph of its probability density looks like a bell. It is also known as called Gaussian distribution, after the German mathematician Carl Gauss who first described it.
How do you plot probability distribution in Python?You first create a plot object ax . Here, you can specify the number of bins in the histogram, specify the color of the histogram and specify density plot option with kde and linewidth option with hist_kws . You can also set labels for x and y axis using the xlabel and ylabel arguments.
|