Clearing and plotting mathplot figure on tkinter

半世苍凉 提交于 2021-02-10 12:57:16

问题


I need some help for my current code. I want to create a window by tkinter and show a plot in a canvas I created by matplotlib before. This point I reached yet. My problem is that I want to clear this canvas by hitting a button. To clear a canvas I suppose to initialize this before I can fill it with the plot.

So my question is: How can I fill a plot in a created canvas?

Below you can find a small code that shows my state of arts.

from matplotlib.figure import Figure 
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)  

def plot(): 
    fig = Figure(figsize = (5, 5), dpi = 100)
    y = [i**2 for i in range(101)]

    # adding the subplot 
    plot1 = fig.add_subplot(111) 

    # plotting the graph 
    plot1.plot(y) 

    # creating the Tkinter canvas 
    # containing the Matplotlib figure 
    output = FigureCanvasTkAgg(fig, master = window)
    output.draw()

    # placing the canvas on the Tkinter window 
    output.get_tk_widget().pack() 

def clear_plot():
    canvas.delete('all') 

# the main Tkinter window 
window = Tk() 

# setting the title 
window.title('Plotting in Tkinter') 

# dimensions of the main window 
window.geometry("700x700") 

canvas = Canvas(window, width=500, height=500) 
canvas.pack()

# button that displays the plot 
plot_button = Button(master = window, command = plot, height = 2, width = 10, text = "Plot") 

clear_button = Button(master = window, command = clear_plot, height = 2, width = 10, text = "clear", background = "yellow")

# place the button 
plot_button.pack() 
clear_button.pack()

# run the gui 
window.mainloop() ```

回答1:


There is no direct way to clear the figure from the math plot canvas. So you can instead clear the canvas by destroying the widget itself using destroy method of tkinter canvas (note you cannot destroy the mathplot canvas itself as it doesn't have any methods such as destroy).

To place math plot canvas on tkinter canvas just set master as canvas object (output = FigureCanvasTkAgg(fig, master = canvas))

(Here is your corrected code)

from tkinter import *
from matplotlib.figure import Figure 
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)  

def plot():
    global output, fig
    
    fig = Figure(figsize = (5, 5), dpi = 100)
    y = [i**2 for i in range(101)]
    # adding the subplot 
    plot1 = fig.add_subplot(111) 

    # plotting the graph 
    plot1.plot(y) 

    # creating the Tkinter canvas 
    # containing the Matplotlib figure 
    output = FigureCanvasTkAgg(fig, master = canvas)
    output.draw()

    # placing the canvas on the Tkinter window 
    output.get_tk_widget().pack() 

def clear_plot():
    global output
    if output:
        for child in canvas.winfo_children():
            child.destroy()
        # or just use canvas.winfo_children()[0].destroy()  
  
    output = None

# the main Tkinter window 
window = Tk() 

output = None
fig = None

# setting the title 
window.title('Plotting in Tkinter') 

# dimensions of the main window 
window.geometry("700x700") 

canvas = Canvas(window, width=500, height=500, bg='white') 
canvas.pack()

# button that displays the plot 
plot_button = Button(master = window, command = plot, height = 2, width = 10, text = "Plot") 

clear_button = Button(master = window, command = clear_plot, height = 2, width = 10, text = "clear", background = "yellow")

# place the button 
plot_button.pack() 
clear_button.pack()

# run the gui 
window.mainloop()

Or you could use

def clear_plot():
    global output
    if output:
        output.get_tk_widget().destroy()
    output = None

here the output is your FigureCanvasTkAgg instance for anyone else looking to achieve this. And you just want to temporarily hide the plot output.get_tk_widget().pack_forget() and to display it again output.get_tk_widget().pack()

update output.get_tk_widget() Return the Tk widget used to implement FigureCanvasTkAgg which means you can also use all the methods of canvas. So,output.get_tk_widget().delete('all') works as well



来源:https://stackoverflow.com/questions/65544881/clearing-and-plotting-mathplot-figure-on-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!