Developing a scatter plot in Python using Turtle
<p>I am working on the "How to think like a computer scientist" course, and am stuck on this question:</p>

<p>Interpret the data file labdata.txt such that each line contains a an x,y coordinate pair. Write a function called plotRegression that reads the data from this file and uses a turtle to plot those points and a best fit line according to the following formulas:</p>

<p>y=yÂ¯+m(xâxÂ¯)</p>

<p>m=âxiyiânxÂ¯yÂ¯âx2iânxÂ¯2</p>

<p>Your program should analyze the points and correctly scale the window using setworldcoordinates so that that each point can be plotted. Then you should draw the best fit line, in a different color, through the points.</p>

<p>Here's what I have so far, but I keep getting an 'int' does not support indexing error. I've been using various online resources and a few solutions on here, but can't seem to get this working properly. </p>

<p>Could anyone help me figure out what to correct? </p>

<pre><code>import turtle


def plotRegression(data):
    win = turtle.Screen()
    win.bgcolor('pink')

    t = turtle.Turtle()
    t.shape('circle')
    # t.turtlesize(0.2)

    x_list, y_list = [int(i[0]) for i in plot_data], [int(i[1]) for i in plot_data]
    x_list, y_list = [float(i) for i in x_list], [float(i) for i in y_list]
    x_sum, y_sum = sum(x_list), sum(y_list)
    x_bar, y_bar = x_sum / len(x_list), y_sum / len(y_list)
    x_list_square = [i ** 2 for i in x_list]
    x_list_square_sum = sum(x_list_square)
    xy_list = [x_list[i] * y_list[i] for i in range(len(x_list))]
    xy_list_sum = sum(xy_list)

    m = (xy_list_sum - len(x_list) * x_bar * y_bar) / (x_list_square_sum - len(x_list) * x_bar ** 2)
    # best y
    y_best = [(y_bar + m * (x_list[i] - x_bar)) for i in range(len(x_list))]

    # plot points

    max_x = max(x_list)
    max_y = max(y_list)
    win.setworldcoordinates(0, 0, max_x, max_y)
    for i in range(len(x_list)):
        t.penup()
        t.setposition(x_list[i], y_list[i])
        t.stamp()

    # plot best y
    t.penup()
    t.setposition(0, 0)
    t.color('blue')
    for i in range(len(x_list)):
        t.setposition(x_list[i], y_best[i])
        t.pendown()

    win.exitonclick()


f = open("labdata.txt", "r")
for aline in f:
    plot_data = map(int, aline.split())
plotRegression(plot_data)
</code></pre>

<p>I think your turtle graphics is a secondary issue -- you're not reading your data in correctly. You're tossing all but the last x, y pair.  And <code>map()</code> isn't your friend here as you'll want to index the result inside <code>plotRegression()</code>.  Also you're accessing <code>plot_data</code> directly in the function instead of the formal parameter <code>data</code> and other details.</p>

<p>Here's my rework of your code, see if it gets you heading in a better direction:</p>

<pre><code>from turtle import Turtle, Screen

def plotRegression(data):

    x_list, y_list = [int(i[0]) for i in data], [int(i[1]) for i in data]
    x_list, y_list = [float(i) for i in x_list], [float(i) for i in y_list]
    x_sum, y_sum = sum(x_list), sum(y_list)
    x_bar, y_bar = x_sum / len(x_list), y_sum / len(y_list)
    x_list_square = [i ** 2 for i in x_list]
    x_list_square_sum = sum(x_list_square)
    xy_list = [x_list[i] * y_list[i] for i in range(len(x_list))]
    xy_list_sum = sum(xy_list)

    m = (xy_list_sum - len(x_list) * x_bar * y_bar) / (x_list_square_sum - len(x_list) * x_bar ** 2)
    # best y
    y_best = [(y_bar + m * (x_list[i] - x_bar)) for i in range(len(x_list))]

    # plot points

    turtle = Turtle(shape = 'circle')

    for i in range(len(x_list)):
        turtle.penup()
        turtle.setposition(x_list[i], y_list[i])
        turtle.stamp()

    # plot best y
    turtle.penup()
    turtle.setposition(0, 0)
    turtle.color('blue')
    for i in range(len(x_list)):
        turtle.setposition(x_list[i], y_best[i])
        turtle.pendown()

    return (min(x_list), min(y_list), max(x_list), max(y_list))

screen = Screen()

screen.bgcolor('pink')

f = open("labdata.txt", "r")

plot_data = []

for aline in f:
    x, y = aline.split()
    plot_data.append((x, y))

# This next line should be something like:

# screen.setworldcoordinates(*plotRegression(plot_data))

# but setworldcoordinates() is so tricky to work with
# that I'm going to leave it at:

print(*plotRegression(plot_data))

# and suggest you trace a rectangle with the return
# values to get an idea what's going to happen to
# your coordinate system

screen.exitonclick()
</code></pre>

python plot statistics turtle-graphics introduction