Skip to content

Instantly share code, notes, and snippets.

@nitstorm
Last active February 1, 2020 10:16
Show Gist options
  • Save nitstorm/9479926 to your computer and use it in GitHub Desktop.
Save nitstorm/9479926 to your computer and use it in GitHub Desktop.
"""
The Introduction to Statistics Wrapper provided by Udacity for their course - https://www.udacity.com/course/st101
"""
from matplotlib import pyplot
from numpy import arange
import bisect
def scatterplot(x,y):
pyplot.plot(x,y,'b.')
pyplot.xlim(min(x)-1,max(x)+1)
pyplot.ylim(min(y)-1,max(y)+1)
pyplot.show()
def barplot(labels,data):
pos=arange(len(data))
pyplot.xticks(pos+0.4,labels)
pyplot.bar(pos,data)
pyplot.show()
def histplot(data,bins=None,nbins=5):
if not bins:
minx,maxx=min(data),max(data)
space=(maxx-minx)/float(nbins)
bins=arange(minx,maxx,space)
binned=[bisect.bisect(bins,x) for x in data]
l=['%.1f'%x for x in list(bins)+[maxx]] if space<1 else [str(int(x)) for x in list(bins)+[maxx]]
displab=[x+'-'+y for x,y in zip(l[:-1],l[1:])]
barplot(displab,[binned.count(x+1) for x in range(len(bins))])
def barchart(x,y,numbins=5):
datarange=max(x)-min(x)
bin_width=float(datarange)/numbins
pos=min(x)
bins=[0 for i in range(numbins+1)]
for i in range(numbins):
bins[i]=pos
pos+=bin_width
bins[numbins]=max(x)+1
binsum=[0 for i in range(numbins)]
bincount=[0 for i in range(numbins)]
binaverage=[0 for i in range(numbins)]
for i in range(numbins):
for j in range(len(x)):
if x[j]>=bins[i] and x[j]<bins[i+1]:
bincount[i]+=1
binsum[i]+=y[j]
for i in range(numbins):
binaverage[i]=float(binsum[i])/bincount[i]
barplot(range(numbins),binaverage)
def piechart(labels,data):
fig=pyplot.figure(figsize=(7,7))
pyplot.pie(data,labels=labels,autopct='%1.2f%%')
pyplot.show()
#Build a scatterplot of Height vs. Weight using the scatterplot function
from plotting import *
Height=[65.78, 71.52, 69.4, 68.22, 67.79, 68.7, 69.8, 70.01, 67.9, 66.78,
66.49, 67.62, 68.3, 67.12, 68.28, 71.09, 66.46, 68.65, 71.23, 67.13, 67.83,
68.88, 63.48, 68.42, 67.63, 67.21, 70.84, 67.49, 66.53, 65.44, 69.52, 65.81,
67.82, 70.6, 71.8, 69.21, 66.8, 67.66, 67.81, 64.05, 68.57, 65.18, 69.66, 67.97,
65.98, 68.67, 66.88, 67.7, 69.82, 69.09]
Weight=[112.99, 136.49, 153.03, 142.34, 144.3, 123.3, 141.49, 136.46,
112.37, 120.67, 127.45, 114.14, 125.61, 122.46, 116.09, 140.0, 129.5, 142.97,
137.9, 124.04, 141.28, 143.54, 97.9, 129.5, 141.85, 129.72, 142.42, 131.55,
108.33, 113.89, 103.3, 120.75, 125.79, 136.22, 140.1, 128.75, 141.8, 121.23,
131.35, 106.71, 124.36, 124.86, 139.67, 137.37, 106.45, 128.76, 145.68, 116.82,
143.62, 134.93]
#Insert your code on the next line
scatterplot(Height,Weight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment