Python Forum
[Tkinter] Update matplotlib plot correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Update matplotlib plot correctly
#1
Evening,

I want to insert a data point externally into an existing plot (f(x) = x, g(x) = x**2). To do this, the x and y coordinates can be entered in entry fields. The user can then press a button to insert the point.

Assuming a data point (x1, y1) is inserted and the user trys to enter a new data point (x2,y2). In this case the GUI should only display the curves (f(x), g(x)) and the point (x2, y2). The last point (x1,y1) should be deleted.


My solution only partially works: Additional points (x,y) can be created, but the old ones are not deleted...

Does any of you know an approach to solve the problem described above.

from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import numpy as np

fig = Figure(figsize = (9, 6), facecolor = "white")

axis = fig.add_subplot(111)
x_values = np.array([1,2,3,4,5,6,7])
axis.plot(x_values, x_values, "-r")
axis.plot(x_values, x_values ** 2, "--g")
axis.grid()

root = tk.Tk()

Label(root, text = "x =" ).grid(row = 0, column = 0)
Label(root, text = "y =" ).grid(row = 1, column = 0)

x = DoubleVar()
y = DoubleVar()

x_entry = Entry(root, textvariable = x).grid(row = 0, column = 1)
y_entry = Entry(root, textvariable = y).grid(row = 1, column = 1)

def plotgraphs():
    axis.plot(x.get(), y.get(), "ko")
    
    canvas = FigureCanvasTkAgg(fig, master = root)
    canvas._tkcanvas.grid(row = 2, column = 1)

Button(root, text = "New Graphs", command = plotgraphs).grid(row = 0, column = 2)

canvas = FigureCanvasTkAgg(fig, master = root)
canvas._tkcanvas.grid(row = 2, column = 1)

root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Update exist plot while change upper limit or lower limit SamLiu 1 949 Feb-05-2023, 10:09 AM
Last Post: SamLiu
Exclamation Update Point Coordinates from Mouse Event in a Plot Jguillerm 2 1,251 Jan-10-2023, 07:53 AM
Last Post: Jguillerm
  How to instantly update the plot by getting values from a Scale widget? OLE 20 6,053 May-18-2022, 02:35 AM
Last Post: OLE
  Update plot by <Return> bind with entry widget Zorro 1 4,093 Mar-09-2019, 12:27 PM
Last Post: Zorro
  Show a plot from matplotlib in a webpage cliquot22 2 5,531 Dec-28-2018, 05:03 AM
Last Post: cliquot22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020