Я пытаюсь очистить холст Tkinter, встроенный в Matplotlib.Я хочу сделать это с помощью кнопки.Как я могу это сделать?
Я перепробовал множество решений, которые были здесь, они не дают код ошибки, но ничего не делают, когда я нажимаю кнопку.Когда я пытаюсь запустить функцию plot_IBL, создается второй график.
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
import os
import xlrd
from xlrd import open_workbook
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from tkinter import ALL
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
class mclass:
def __init__(self, window):
self.window = window
self.canvas = None
self.button = Button (window, text="Plot Surface", command=self.plot_IBL)
self.clear_button = Button(window, text="Clear Space", command = self.clear_space)
self.button.pack(side = "left")
self.clear_button.pack(side = "left")
def clear_space(self):
fig = Figure(figsize=(4,4))
self.canvas = FigureCanvasTkAgg(fig, master=self.window)
self.canvas.draw()
def plot_IBL (self):
#File selection dialog
fig = Figure(figsize=(4,4))
self.canvas = FigureCanvasTkAgg(fig, master=self.window)
self.canvas.get_tk_widget().pack()
self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)
a = fig.add_subplot(111, projection = "3d")
### CALCULATIONS DONE IN THIS WHITE SPACE
max_x = x_prime.values.max()
max_y = y_prime.values.max()
max_z = z_prime.values.max()
###### Convert XY to negative values to plot entire horn
x_negprime = x_prime * (-1)
y_negprime = y_prime * (-1)
####Create Triangles to plot compelete surface
tri = mtri.Triangulation(x_prime,y_prime)
tri2 = mtri.Triangulation(x_negprime,y_prime)
tri3 = mtri.Triangulation(x_negprime, y_negprime)
tri4 = mtri.Triangulation(x_prime, y_negprime)
a.set_title (base, fontsize=16)
a.set_ylabel("Y", fontsize=14)
a.set_xlabel("X", fontsize=14)
a.set_zlabel("Z", fontsize=14)
a.plot_trisurf(x_prime, y_prime, z_prime, triangles = tri.triangles)
a.plot_trisurf(x_negprime, y_prime, z_prime, triangles = tri.triangles)
a.plot_trisurf(x_negprime, y_negprime, z_prime, triangles = tri.triangles)
self.canvas.draw()
window= Tk()
window.wm_title("3D Plot Surface")
start= mclass (window)
window.mainloop()
``
I expect the plot to be cleared and when i run my plot_IBL function I do not want a second plot to appear. I want the same plot to be used.