Bell Curve (Normal Distribution) Using Numpy

Create a data set of values that fit a bell curve (normal distribution). Plot the data as a histogram and a line.

#!/usr/bin/python3 # ========================================================= # see https://realpython.com/numpy-random-normal/ # ========================================================= import secrets import numpy as np import matplotlib.pyplot as plt # ---- create a random number generator # ---- (seed is a very large number) seed = secrets.randbits(128) rng = np.random.default_rng(seed) # ---- create 1000 random number that fit a bell curve # ---- (normal distribution) # ---- loc is the mean # ---- scale is the standard deviation # ---- size is the number of samples numbers = rng.normal(loc=0,scale=4,size=1000) min_num = min(numbers) max_num = max(numbers) sigma = np.std(numbers) mean = np.mean(numbers) print(f'type(numbers) = {type(numbers)}') print(f'min number = {min_num}') print(f'max number = {max_num}') print(f'sigma = {sigma} (standard deviation)') print(f'mean = {mean}') # ---- collect numbers into 30 bins and plot a histogram # ---- density=True means the area under the histogram is 1 count,bins,ignore = plt.hist(numbers,30,density=True) print(f'type(bins) = {type(bins)}') print(f'len(bins) = {len(bins)}') # ---- create an np.array containing the bell curve's y values # ---- a y value for each x (bins) value y = 1/(sigma*np.sqrt(2*np.pi))* \ np.exp(-(bins-mean)**2/(2*sigma**2)) print(f'type(y) = {type(y)}') print(f'len(y) = {len(y)}') # ---- plot (bins,y) plt.plot(bins,y,linewidth=2,color='r') plt.show()

Equation for the X,Y Coordinates of a Bell Curve

Y = Ke-(X-M)2/(2σ2)

X,Yare the curve's x,y coordinates (used for plotting, etc.)
Kis the maximum Y coordinate; used to scale the Y coordinates
(height in Y units)
Mis the curve's mathematical mean (X coordinate of the mean)
σis the curve's standard deviation; determines how fat or skinny
the curve is (width in X units)
eis Euler's number; is a constant; is an irrational number
(defined in the Python numpy module and other libraries)
From: math.stackexchange.com

With this equation the user can: