Plots nicely, shows important info in text box including Info
This commit is contained in:
48
normdist.py
48
normdist.py
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
# From https://www.geeksforgeeks.org/python/how-to-plot-a-normal-distribution-with-matplotlib-in-python/
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import scipy.stats
|
||||
from scipy.stats import norm
|
||||
|
||||
## Data goes here for now --foley
|
||||
data = np.array([1, 1.1, 0.9, 1, 1, 0.9, 0.9])
|
||||
lowerbound = 0.9
|
||||
upperbound = 1.0
|
||||
@@ -11,18 +11,46 @@ upperbound = 1.0
|
||||
mean = data.mean()
|
||||
stddev = data.std(ddof=1)
|
||||
# Delta Degrees of Freedom: ddof=0 for population, ddof=1 for sample std dev
|
||||
x = np.linspace(mean-3*stddev, mean+3*stddev, 1000)
|
||||
y = scipy.stats.norm.pdf(x, mean, stddev)
|
||||
plt.plot(x, y, 'b-', label='Normal Distribution')
|
||||
prob = norm.cdf(upperbound, mean, stddev) - norm.cdf(lowerbound, mean, stddev)
|
||||
#print("probability: %f", prob)
|
||||
info = -np.emath.log2(prob)
|
||||
#print("information content: %f bits", info)
|
||||
## place text on plot: https://matplotlib.org/3.3.4/gallery/recipes/placing_text_boxes.html
|
||||
fig, ax = plt.subplots()
|
||||
textstr = '\n'.join((
|
||||
r'$n=%d$' % (len(data)),
|
||||
r'$\mu=%.2f$' % (mean, ),
|
||||
r'$P=%.2f$' % (prob, ),
|
||||
r'$I=%.2f$' % (info, )))
|
||||
# these are matplotlib.patch.Patch properties
|
||||
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
|
||||
|
||||
# place a text box in upper left in axes coords
|
||||
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
|
||||
verticalalignment='top', bbox=props)
|
||||
|
||||
x = np.linspace(mean-3*stddev, mean+3*stddev, 500)
|
||||
y = norm.pdf(x, mean, stddev)
|
||||
plt.axvline(x=mean, color="green", linestyle="dashed", label="mean")
|
||||
plt.axvline(lowerbound, color="red")
|
||||
plt.axvline(upperbound, color="red")
|
||||
|
||||
plt.plot(x, y, 'b-', label='Normal distribution')
|
||||
#yt = scipy.stats.t.pdf(x, len(data)-1, mean, stddev)
|
||||
#plt.plot(x, yt, 'g-', label='T Distribution')
|
||||
coloredregion = (x >= lowerbound) & ( x <= upperbound ) #select x values
|
||||
plt.vlines([lowerbound, upperbound], 0, plt.ylim()[1], color="red")#ylim returns [bot, top]
|
||||
|
||||
plt.fill_between(x, 0, y, where=coloredregion, color="grey", alpha=0.5, label="Design Range")
|
||||
plt.fill_between(x, 0, y, where=coloredregion, color="grey", alpha=0.5, label="Design range")
|
||||
plt.xlabel('X')
|
||||
plt.ylabel('Probability Density')
|
||||
plt.ylabel('Probability density')
|
||||
plt.legend()
|
||||
plt.grid(True)
|
||||
plt.show()
|
||||
|
||||
top = plt.ylim()[1]
|
||||
|
||||
|
||||
plt.show()
|
||||
# annotate values on X after drawing the graphs
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user