#! /usr/bin/python3
# ===================================================================
# draw/plot a bell curve
# ===================================================================
# Formula from:
#
# statistics.about.com/od/Formulas/ss/
# The-Normal-Distribution-Or-Bell-Curve.htm
#
# ===================================================================
#
# Notes:
#
# 1. The bell curve formula normalizes the area under the
# curve to 1.
# 2. All Y values are bell curve formula values and less than 1.
# 3. When plotting, the X values are scaled.
# 4. When plotting, the Y values are scaled.
# 5. Some day re-code to read a data file.
#
# ===================================================================
import numpy as np
import draw_xy_axes as ax
import coordinate_conversion as cc
import user_interface as ui
from graphics import *
# ---- global variables
xscale = 50.0 # scale bell curve plot
yscale = 50.0 # scale bell curve plot
mean = 0.0 # plot mean value
sd = 1.0 # plot standard deviation
# ---- bell curve x values (standard deviation units)
xdata = [ -3.00,-2.75,-2.50,-2.25,
-2.00,-1.75,-1.50,-1.25,
-1.00,-0.75,-0.50,-0.25,
0.00,
0.25, 0.50, 0.75, 1.00,
1.25, 1.50, 1.75, 2.00,
2.25, 2.50, 2.75, 3.00 ]
# -------------------------------------------------------------------
# ---- return the bell curve y value for a given x value
# ---- x value (standard deviation units)
# ---- mean (center of distribution)
# ---- sd standard deviation
# -------------------------------------------------------------------
def BellCurveValue(x,m,sd):
y = (1.0/sd*np.sqrt(2.0*np.pi)) * \
pow(np.e,-pow(x-m,2.0)/(2.0*sd*sd))
return y
# -------------------------------------------------------------------
# ---- plot the bell curve
# ---- xdata bell curve x points to plot
# ---- xscale x scale factor
# ---- yscale y scale factor
# ---- m mean value (middle of bell curve)
# ---- sd standard deviation
# -------------------------------------------------------------------
def PlotBellCurve(xdata,xscale,yscale,m,sd):
win = GraphWin("Bell Curve", 801, 801)
win.setBackground("white")
ax.draw_xy_axes(win,True)
for x in xdata:
y = BellCurveValue(x,m,sd)
cy = round(y * yscale)
cx = round(x * xscale)
(wx,wy) = cc.center_to_win_coords(cx,cy,win.width,win.height)
##l = Line(Point(wx,wy),Point(wx+1,wy+1))
##l.setWidth(2)
##l.setFill('black')
##l.draw(win)
c = Circle(Point(wx,wy),4)
c.setFill('black')
c.draw(win)
return win
# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------
if __name__ == '__main__':
# ---- plot bell curve
win = PlotBellCurve(xdata,xscale,yscale,mean,sd)
ui.pause()
win.close()