Skip to content

Instantly share code, notes, and snippets.

@newton-migosi
Last active July 20, 2018 11:38
Show Gist options
  • Save newton-migosi/df736ab094da29bc7c679a042bff88a3 to your computer and use it in GitHub Desktop.
Save newton-migosi/df736ab094da29bc7c679a042bff88a3 to your computer and use it in GitHub Desktop.
Plotting scatter plot and line of best fit using python (turtle and math modules). Run the code online: https://trinket.io/python/d43cc2dddf
44 71
79 37
78 24
41 76
19 12
19 32
28 36
22 58
89 92
91 6
53 7
27 80
14 34
8 81
80 19
46 72
83 96
88 18
96 48
77 67
import turtle
import math
def main(datafile):
dataset = retrieveData(datafile)
wn = turtle.Screen()
wn.setworldcoordinates(0,0,1,1)
kurt = turtle.Turtle()
plotRegression(kurt, dataset)
def retrieveData(filename):
data = []
with open(filename, 'r') as datafile:
for line in datafile:
datapoint = tuple([int(i) for i in line.split()])
data.append(datapoint)
return data
def plotRegression(t, dataset):
plotPoints(t, dataset)
m, c = bestfit(dataset)
plotLine(t, m, c)
def plotPoints(t, dataset):
t.up()
for datapoint in dataset:
point = tuple([cord/100 for cord in datapoint])
t.goto(*point); t.dot()
t.goto(0,0); t.down()
def plotLine(t, m, c):
t.up(); t.goto(0, c); t.down()
angle = math.degrees(math.atan(m))
t.seth(angle)
while t.xcor() < 1 and t.ycor() < 1:
t.forward(1)
def bestfit(dataset):
average = lambda a : sum(a)/len(a)
x_vals, y_vals = zip(*dataset)
sum_of_products = sum([x*y for x, y in dataset])
sum_of_x_squares = sum([x*x for x in x_vals])
x_mean = average(x_vals)
y_mean = average(y_vals)
n = len(dataset)
m = ((sum_of_products - (n * x_mean * y_mean))/
(sum_of_x_squares - (n * x_mean ** 2)))
c = (y_mean - (m * x_mean))/100
return m, c
if __name__ == '__main__':
main('labdata.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment