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 *
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
points[0].append(int(x_coord.get()))
points[1].append(int(y_coord.get()))
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
print(points)
if graph != None:
graph.remove()
graph = None
points[0] = []
points[1] = []
plt.scatter(points[0], points[1], color="red")
plt.savefig("current_graph.png") #640 x 480
create_image("current_graph.png", 0, -20)
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 :)
CodePudding user response:
You should properly clear plot before plotting anything else.
Matplotlib has clf method for that.
Just change Your reset_graph
function to:
def reset_graph():
global graph
print(points)
if graph != None:
graph.remove()
graph = None
points[0] = []
points[1] = []
# Clear the current figure
plt.clf()
plt.scatter(points[0], points[1], color="red")
plt.savefig("current_graph.png") #640 x 480
create_image("current_graph.png", 0, -20)