0

I have written a very simple code to have a scatter plot. I wondered to know how I can replace the figure by introducing a new y-axis through a second function, and updating the figure. in this example, I can plot based on the values for x, y1. I want to know if I got new values such as y2 through another function, how to update the figure?

from tkinter import *
import matplotlib.pyplot as plt

root = Tk()

def plot():
    x = [1,2,3,4,5,6,7,8,9]
    y1 = [1,2,3,4,5,6,7,8,1]
    plt.scatter(x, y1)

    plt.title('Test')
    plt.xlabel('x')
    plt.ylabel('y')

    plt.show()

def update():
    y2 = [1, 2, 3, 4, 5, 4, 3, 2, 1]

my_button1 = Button(root, text="plot", command=plot)
my_button1.pack()

my_button2 = Button(root, text="update", command=update)
my_button2.pack()

root.mainloop()
5
  • you could use a global variable.
    – Barmar
    Mar 2, 2022 at 20:46
  • @Barmar I dont want to pass the variable over the first function. I look for a way to update the variable from the outside the first function.
    – Luke20
    Mar 2, 2022 at 20:48
  • you can't access local variables from outside the function.
    – Barmar
    Mar 2, 2022 at 20:50
  • @Barmer I have seen we can use config() to change the values inside a widget. Isn't there a way to do that for variables?
    – Luke20
    Mar 2, 2022 at 20:52
  • No, it's not possible to do that for variables. The widget is in a class, it provides methods to change the values of attributes. You could write a class for your application.
    – Barmar
    Mar 2, 2022 at 20:54

3 Answers 3

2

Make the Y values a global variable. You can also make x automatically adapt to the length of this, instead of hard-coding 9 elements.

from tkinter import *
import matplotlib.pyplot as plt

root = Tk()

y_values = [1,2,3,4,5,6,7,8,1]

def plot():
    x = list(range(1, len(y_values)+1))
    plt.scatter(x, y_values)

    plt.title('Test')
    plt.xlabel('x')
    plt.ylabel('y')

    plt.show()

def update():
    global y_values
    y_values = [1, 2, 3, 4, 5, 4, 3, 2, 1]

my_button1 = Button(root, text="plot", command=plot)
my_button1.pack()

my_button2 = Button(root, text="update", command=update)
my_button2.pack()

root.mainloop()
2
  • @Barmer, Thanks for your reply. this code doesn't update the figure
    – Luke20
    Mar 2, 2022 at 20:54
  • You may need to reset the figure or delete the old one and create a new one. I don't know matplotlib.
    – Barmar
    Mar 2, 2022 at 20:55
1

You need to use a global y list. The plot() function reads that global variable and the update() function updates that global variable (and then calls plot() after clearing the figure with plt.clf()) :

from tkinter import *
import matplotlib.pyplot as plt

root = Tk()

x = [1,2,3,4,5,6,7,8,9]
y = [1,2,3,4,5,6,7,8,1]

def plot():
    # if you want it to be reversible, add :
    # global y
    # y = [1,2,3,4,5,6,7,8,1]
    # plt.clf()

    plt.scatter(x, y)

    plt.title('Test')
    plt.xlabel('x')
    plt.ylabel('y')

    plt.show()

def update():
    global y
    y = [1, 2, 3, 4, 5, 4, 3, 2, 1]
    plt.clf()
    plot()

my_button1 = Button(root, text="plot", command=plot)
my_button1.pack()

my_button2 = Button(root, text="update", command=update)
my_button2.pack()

root.mainloop()
1
  • Thanks, indeed it works for this example. however, I still have a problem with my case. I think I need another post to be more precise with my case.
    – Luke20
    Mar 2, 2022 at 21:25
0

If you move the data into parameters, you can invoke the function twice, which will plot the new graph after the first window is dismissed:

x = [1,2,3,4,5,6,7,8,9]
y = [1,2,3,4,5,6,7,8,1]

def plot(x, y):
    plt.scatter(x, y)
    plt.title('Nuage de points avec Matplotlib')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.savefig('ScatterPlot_01.png')
    plt.show()

plot(x, y)

y2 = [1,2,3,4,5,4,3,2,1]
plot(x, y2)

If you want to have a different title or save to a different .png, you can further add parameters to plot():

title = 'Nuage de points avec Matplotlib part2'
filename = 'ScatterPlot_02.png'

def plot(x, y, title, filename):
    plt.scatter(x, y)
    plt.title(title)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.savefig(filename )
    plt.show()

plot(x, y2, title, filename)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.