Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Thu May 8 23:16:31 2014
4
5
@author: rlabbe
6
"""
7
8
import math
9
import matplotlib.pylab as pylab
10
import matplotlib.pyplot as plt
11
import numpy as np
12
import filterpy.stats as stats
13
14
def plot_height_std(x, lw=10):
15
m = np.mean(x)
16
s = np.std(x)
17
18
for i, height in enumerate(x):
19
plt.plot([i+1, i+1], [0, height], color='k', lw=lw)
20
plt.xlim(0,len(x)+1)
21
plt.axhline(m-s, ls='--')
22
plt.axhline(m+s, ls='--')
23
plt.fill_between((0, len(x)+1), m-s, m+s,
24
facecolor='yellow', alpha=0.4)
25
plt.xlabel('student')
26
plt.ylabel('height (m)')
27
plt.show()
28
29
30
def plot_gaussian (mu, variance,
31
mu_line=False,
32
xlim=None,
33
xlabel=None,
34
ylabel=None):
35
36
xs = np.arange(mu-variance*2,mu+variance*2,0.1)
37
ys = [stats.gaussian (x, mu, variance)*100 for x in xs]
38
plt.plot (xs, ys)
39
if mu_line:
40
plt.axvline(mu)
41
if xlim:
42
plt.xlim(xlim)
43
if xlabel:
44
plt.xlabel(xlabel)
45
if ylabel:
46
plt.ylabel(ylabel)
47
plt.show()
48
49
def display_stddev_plot():
50
xs = np.arange(10,30,0.1)
51
var = 8; stddev = math.sqrt(var)
52
p2, = plt.plot (xs,[stats.gaussian(x, 20, var) for x in xs])
53
x = 20+stddev
54
y = stats.gaussian(x, 20, var)
55
plt.plot ([x,x], [0,y],'g')
56
plt.plot ([20-stddev, 20-stddev], [0,y], 'g')
57
y = stats.gaussian(20,20,var)
58
plt.plot ([20,20],[0,y],'b')
59
ax = plt.axes()
60
ax.annotate('68%', xy=(20.3, 0.045))
61
ax.annotate('', xy=(20-stddev,0.04), xytext=(x,0.04),
62
arrowprops=dict(arrowstyle="<->",
63
ec="r",
64
shrinkA=2, shrinkB=2))
65
ax.xaxis.set_ticks ([20-stddev, 20, 20+stddev])
66
ax.xaxis.set_ticklabels(['$-\sigma$','$\mu$','$\sigma$'])
67
ax.yaxis.set_ticks([])
68
plt.show()
69
70
if __name__ == '__main__':
71
display_stddev_plot()
72