Reset button removes last added point

So I’m working on something related with graphs on linear regression. So far, I have created this simple program that creates a graph, allows you to add points, randomly generate 20 points, and a reset button. The reset button works fine when I generate a random graph. That is, it resets the graph to nothing. However, if I add let’s say 3 points (3,4), (6,7), (1,2), and I press the reset button, it only removes the latest point I graphed (in this case (1,2)). Can someone help me out?

from tkinter import *
from tkinter import messagebox

import matplotlib.pyplot as plt
import PIL
import random


points = [[],[]]

screen = Tk()
screen.title("Linear Regression")
screen.geometry("875x900")
graph = None

def point():
    global graph
    x = x_coord.get()
    y = y_coord.get()
    if not x.isdigit() or not y.isdigit():
        messagebox.showinfo("Whoah there", "Please make sure both x and y value are integers")
        x_coord.delete(0, "end")
        y_coord.delete(0, "end")
    else:
        points[0].append(int(x))
        points[1].append(int(y))
        x_coord.delete(0, "end")
        y_coord.delete(0, "end")
        graph = plt.scatter(points[0], points[1], color="red")
        plt.savefig("current_graph.png") #640 x 480
        create_image("current_graph.png", 0, -20)

def gen_points():
    points[0] = [i for i in range(1,21)]
    points[1] = [random.randint(1,20)for i in range(20)]
    graph = plt.scatter(points[0], points[1], color="red")
    plt.savefig("current_graph.png") #640 x 480
    create_image("current_graph.png", 0, -20)
    graph.remove()
    points[0] = []
    points[1] = []

def create_image(image_file, posx, posy):
    global graph_image
    image_object = PIL.Image.open(image_file)
    canvas = Canvas(screen, width = image_object.size[0], height = image_object.size[1])
    canvas.place(x = posx, y = posy)
    graph_image = PIL.ImageTk.PhotoImage(image_object)
    canvas.create_image(0, 0, anchor=NW, image = graph_image)

def reset_graph():
    global graph
    if graph != None:
        graph.remove()
    points[0] = []
    points[1] = []
    g = plt.scatter([0], [0], color="red")
    g.remove()
    #plt.savefig("wtf.png")
    plt.savefig("current_graph.png") #640 x 480
    create_image("current_graph.png", 0, -20)

def linear_regression(points):
    x = points[0]
    y = points[1]
    avg_x = sum(x) / len(x)
    x_differences = [avg_x - value for value in x]
    avg_x_square = [x ** 2 for x in x_differences]

    avg_y = sum(y) / len(y)
    y_differences = [avg_y - value for value in y]

    final_thing = [x_differences[i] * y_differences[i] for i in range(len(x_differences))]

    slope = round(sum(final_thing) / sum(avg_x_square), 3)

    intercept = round(avg_y - slope * avg_x, 3)

    print(f"y = {slope}x + {intercept}")

    return (slope, intercept)


plt.scatter(points[0], points[1], color="red")
plt.savefig("current_graph.png") #640 x 480
create_image("current_graph.png", 0, -20)

x_label = Label(screen, text="X:")
x_label.place(x = 640, y = 5)
x_coord = Entry(screen, width = 10)
x_coord.place(x=655, y=5)

y_label = Label(screen, text="Y:")
y_label.place(x = 720, y = 5)
y_coord = Entry(screen, width = 10)
y_coord.place(x=735, y=5)

random_gen = Button(screen, text = "Generate Random Points", command = gen_points)
random_gen.place(x = 655, y = 30)

reset_button = Button(screen, text = "Reset", command = reset_graph)
reset_button.place(x = 805, y = 30)

add_point = Button(screen, text = "Add Point", command = point)
add_point.place(x=805, y = 0)




screen.mainloop()

Also any suggestions on how I can improve my program in general would be appreciated :slight_smile: