Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib plot in Tkinter - every update adds new NavigationToolbar?

I am working on a Tkinter-GUI to interactively generate Matplotlib-plots, depending on user-input. For this end, it needs to re-plot after the user changes the input.

I have gotten it to work in principle, but would like to include the NavigationToolbar. However, I cannot seem to get the updating of the NavigationToolbar to work correctly.

Here is a basic working version of the code (without the user input Entries):

# Import modules
from Tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

# global variable: do we already have a plot displayed?
show_plot = False

# plotting function
def plot(x, y):
    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
    ax1.plot(x,y)
    return fig

def main():
    x = np.arange(0.0,3.0,0.01)
    y = np.sin(2*np.pi*x)
    fig = plot(x, y)

    canvas = FigureCanvasTkAgg(fig, master=root)
    toolbar = NavigationToolbar2TkAgg(canvas, toolbar_frame)

    global show_plot
    if show_plot:
        #remove existing plot and toolbar widgets
        canvas.get_tk_widget().grid_forget()
        toolbar_frame.grid_forget()

    toolbar_frame.grid(row=1,column=1)
    canvas.get_tk_widget().grid(row=0,column=1)
    show_plot=True

# GUI
root = Tk()

draw_button = Button(root, text="Plot!", command = main)
draw_button.grid(row=0, column=0)

fig = plt.figure()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0,column=1)
toolbar_frame = Frame(root)
toolbar_frame.grid(row=1,column=1)

root.mainloop()

Pressing "Plot!" once generates the plot and the NavigationToolbar. Pressing it a second time replots, but generates a second NavigationToolbar (and another every time "Plot!" is pressed). Which sounds like grid_forget() is not working. However, when I change

    if show_plot:
        #remove existing plot and toolbar widgets
        canvas.get_tk_widget().grid_forget()
        toolbar_frame.grid_forget()

    toolbar_frame.grid(row=1,column=1)
    canvas.get_tk_widget().grid(row=0,column=1)
    show_plot=True

to

    if show_plot:
        #remove existing plot and toolbar widgets
        canvas.get_tk_widget().grid_forget()
        toolbar_frame.grid_forget()
    else:
        toolbar_frame.grid(row=1,column=1)
    canvas.get_tk_widget().grid(row=0,column=1)
    show_plot=True

then the NavigationToolbar does vanish when "Plot!" is pressed a second time (but then there is, as expected, no new NavigationToolbar to replace the old). So grid_forget() is working, just not as expected.

What am I doing wrong? Is there a better way to update the NavigationToolbar?

Any help greatly appreciated! Lastalda

Edit:

I found that this will work if you destroy the NavigationToolbar instead of forgetting it. But you have to completely re-create the widget again afterwards, of course:

canvas = FigureCanvasTkAgg(fig, master=root)
toolbar_frame = Frame(root)

global show_plot
if show_plot: # if plot already present, remove plot and destroy NavigationToolbar

    canvas.get_tk_widget().grid_forget()
    toolbar_frame.destroy()

toolbar_frame = Frame(root)
toolbar = NavigationToolbar2TkAgg(canvas, toolbar_frame)
toolbar_frame.grid(row=21,column=4,columnspan=3)
canvas.get_tk_widget().grid(row=1,column=4,columnspan=3,rowspan=20)

show_plot = True

However, the updating approach showed by Hans below is much nicer since you don't have to destroy and recreate anything. I just wanted to highlight that the issue with my approach (apart from the inelegance and performance) was probably that I didn't use destroy().

like image 607
CodingCat Avatar asked Oct 31 '25 00:10

CodingCat


1 Answers

A slightly different approach might be to reuse the figure for subsequent plots by clearing & redrawing it. That way, you don't have to destroy & regenerate neither the figure nor the toolbar:

from Tkinter import Tk, Button
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

# plotting function: clear current, plot & redraw
def plot(x, y):
    plt.clf()
    plt.plot(x,y)
    # just plt.draw() won't do it here, strangely
    plt.gcf().canvas.draw()

# just to see the plot change
plotShift = 0
def main():
    global plotShift

    x = np.arange(0.0,3.0,0.01)
    y = np.sin(2*np.pi*x + plotShift)
    plot(x, y)

    plotShift += 1

# GUI
root = Tk()

draw_button = Button(root, text="Plot!", command = main)
draw_button.grid(row=0, column=0)

# init figure
fig = plt.figure()

canvas = FigureCanvasTkAgg(fig, master=root)
toolbar = NavigationToolbar2TkAgg(canvas, root)
canvas.get_tk_widget().grid(row=0,column=1)
toolbar.grid(row=1,column=1)

root.mainloop()
like image 104
Hans Avatar answered Nov 02 '25 13:11

Hans