pythonmatplotlibtkintergraph-drawing

How to update a graph created by matplotlib in tkinter


I have an app created by using tkinter. There is just a window in which I'm typing X and Y. Having these x and y, my app should create a graph using matplotlib.

First time it works correctly, but others no. I mean, I open the app, input X and Y, click button and the app creates graph, then I input new X and Y, click button and this doesn't work, graph is the same, not new. I've searched many sites, but no one of solutions works.

a part of code looks like:

from tkinter import *
import matplotlib

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure


window_width = 1000
window_height = 600
root = Tk()

global_fig = None


def create_draw(x_dots, y_dots):
    global global_fig
    if global_fig is not None:
        global_fig.clf()

    global_fig = Figure(figsize=(5, 4), dpi=100)

    global_fig.add_subplot(111).plot(x_dots, y_dots)

    canvas = FigureCanvasTkAgg(global_fig, master=root)  # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)

    toolbar = NavigationToolbar2Tk(canvas, root)
    toolbar.update()
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
    root.update()
    global_fig.canvas.draw()

root.mainloop()

the sense of problem is:

root.mainloop()
create_draw([3,4], [2,3])

after typing it I see a graph, then I type

create_draw([-1, -2], [-3, -4])

And I should see another graph, but I see old graph in tkinter.


Solution

  • To reuse a plotting area, you need to:

    1. clear the current figure
    2. Add new data to the plot
    3. Redraw the canvas

    Below is how it can work with some changes to your code:

    from tkinter import *
    import numpy as np
    
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
    from matplotlib.figure import Figure
    
    window_width = 1000
    window_height = 600
    
    root = Tk()
    
    fig = Figure(figsize=(5, 4), dpi=100)
    
    canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
    toolbar = NavigationToolbar2Tk(canvas, root)
    
    def draw_chart():
        fig.clear()
        fig.add_subplot(111).plot(np.random.randint(1,10,5), np.random.randint(10,20,5)) #generate random x/y
        canvas.draw_idle()
    
    Button(root,text="Draw",command=draw_chart).pack()
    
    root.mainloop()