Skip to content

Instantly share code, notes, and snippets.

@matsuken92
Last active February 19, 2022 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsuken92/449ab3b4f382a9ffdbb3 to your computer and use it in GitHub Desktop.
Save matsuken92/449ab3b4f382a9ffdbb3 to your computer and use it in GitHub Desktop.
Graph for explanation of Standard Deviation.
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import animation as ani
import numpy as np
import sys
plt.style.use('ggplot')
x = [96, 63, 85, 66, 91, 89, 77]
m = np.mean(x)
plt.figure(figsize=(11,2))
plt.yticks([])
plt.xlim(60,100)
plt.ylim(-1,1)
plt.scatter(x, np.zeros_like(x), s=50,zorder=100)
plt.scatter(np.mean(x), [0], s=100, c="r",zorder=100)
for i in range(len(x)):
plt.text(x[i],-0.3,"x{}".format(i+1),size=12)
plt.text(x[i],-0.5,"={}".format(x[i]),size=12)
plt.text(m,-0.3,"average",size=12)
plt.text(m,-0.6,"={}".format(m),size=12)
plt.plot([0,100],[0,0],c="k",zorder=10)
plt.show()
x = [96, 63, 85, 66, 91, 89, 77]
m = np.mean(x)
plt.figure(figsize=(11,4))
plt.yticks([])
plt.xlim(60,100)
plt.ylim(-1,5)
plt.scatter(x, np.zeros_like(x), s=50,zorder=100)
plt.scatter(np.mean(x), [0], s=100, c="r",zorder=100)
for i in range(len(x)):
plt.text(x[i]+.2,-0.4,"x{}".format(i+1),size=12)
plt.text(x[i]+.2,-0.8,"={}".format(x[i]),size=12)
plt.text(m+.1,-0.4,"average",size=12)
plt.text(m+.1,-0.8,"={}".format(m),size=12)
plt.plot([0,100],[0,0],c="k",zorder=10)
plt.plot([0,100],[0,0],"k",zorder=10)
plt.plot([m,m],[-5,10],"k--",zorder=10)
for i, p, sd in zip(range(len(x)),[1.5, 4.5, 3, 4 ,2 ,2.5 ,3.5],[ 15, -18, 4, -15, 10, 8, -4]):
plt.plot([x[i],x[i]],[-5,10],"k--",zorder=10)
plt.plot([x[i],m],[p,p],"r",zorder=10)
plt.text(x[i]+.1,p-.3,"{}".format(sd),size=12)
plt.show()
x = np.linspace(-2, 2, 500)
y2 = abs(x)
fig = plt.figure(figsize=(10,6))
plt.plot([-10],[-1])
plt.plot(x,y2)
plt.xlim(-2,2)
plt.ylim(0,2)
x = np.linspace(-2, 2, 500)
y = x**2
y2 = abs(x)
fig = plt.figure(figsize=(10,6))
plt.plot(x,y)
plt.plot(x,y2)
plt.xlim(-2,2)
plt.ylim(0,2)
N = 0
num_frame = 26
unit = N/num_frame
data = []
def animate(nframe):
global num_frame, N
plt.clf()
#n = (nframe+1) * unit
v = 1 + 0.1 * nframe
data.extend(np.random.normal(0, v, 350))
N += 350
m = np.mean(data)
sd = np.sqrt(np.var(data))
plt.title("Normal Distribution. datasize={2:4d}, average={0:.3f}, sd={1:.3f}".format(m, sd,N))
plt.xlim(-10,10)
plt.ylim(0,1200)
plt.hist(data, bins=50, color="lightblue")
plt.scatter(m, [0], s=100, c="r",zorder=100)
plt.plot([0, sd],[450,450], c="r", lw=2)
plt.text(0, 420,"sd={0:.3f}".format(sd),size=12)
plt.text(-.05, 30,"m={0:.3f}".format(m),size=12)
fig = plt.figure(figsize=(10,6))
anim = ani.FuncAnimation(fig, animate, frames=num_frame, blit=True)
anim.save('norm_hist_anim.gif', writer='imagemagick', fps=3, dpi=64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment