changing from python 3.8 to 3.9 disrupted raw.plot() in tkinter canvas

Python 3.9, tkinter gui, matplotlib, windows11. mne 1.3.1
Using python 3.8 plotting was smoooth. Now , when raw EEG plots in canvas inside a tk.Frame, the plot has its inferior part outside (bigger than) canvas, then I move gui a little or go canvas fullscreen the plot resizes correctly. bellow script.
Any suggestion or tip?

class EEGPlot:
def init(self):
pass

def plot_eeg(self, my_canvas, file_path):
    # create an instance of the EEGPlot class
    eeg_plot = EEGPlot()
    
    eeg_plot.plot_eeg_canvas(my_canvas, file_path)


def plot_eeg_canvas(self, my_canvas,file_path):
    # check if file path exists
    if not os.path.isfile(file_path):
        tk.messagebox.showerror("Error", f"File path {file_path} does not exist.")
        return
    
    # this says the start point in scrollbar plot() is zero (left) when reading EEG
    start_var = 0
    
    # Load raw data from file path using MNE-Python
    raw = mne.io.read_raw_fif(file_path)
    
    # Clear the plot and create a new figure
    plt.clf()
    
    fig_home = raw.plot(events=None, duration=12.0, start=start_var, n_channels=20, bgcolor='w', color=None,
                        bad_color=(0.8, 0.8, 0.8), event_color='cyan', remove_dc=True, order=None,
                        show_options=True, title=None, show=False, block=False, highpass=None, lowpass=None,
                        filtorder=4, clipping=1.5, show_first_samp=False, proj=True, group_by='type',
                        butterfly=False,
                        decim='auto', noise_cov=None, event_id=None, show_scrollbars=True, show_scalebars=True,
                        verbose=None)

    fig_home.tight_layout()
    
    # Create a canvas widget to display the figure
    canvas_widget = FigureCanvasTkAgg(fig_home, master=my_canvas)
    canvas_widget.draw()
    # Configure the canvas widget to expand and fill the canvas frame
    canvas_widget.get_tk_widget().place(relx=0, rely=0, relwidth=1, relheight=1)

I figured it out, in case it helps anyone, the solution was create another frame inside my_canvas frame, there it goes (with commentaries):
class EEGPlot:
def init(self):
pass

def plot_eeg(self, my_canvas, file_path):
    # create an instance of the EEGPlot class
    eeg_plot = EEGPlot()
    
    eeg_plot.plot_eeg_canvas(my_canvas, file_path)


def plot_eeg_canvas(self, my_canvas,file_path):
    # check if file path exists
    if not os.path.isfile(file_path):
        tk.messagebox.showerror("Error", f"File path {file_path} does not exist.")
        return
    
    # this says the start point in scrollbar plot() is zero (left) when reading EEG
    start_var = 0
    
    # Load raw data from file path using MNE-Python
    raw = mne.io.read_raw_fif(file_path)
    
    # Clear the plot and create a new figure
    plt.clf()

    # Set the figure size using rcParams
    # mpl.rcParams['figure.figsize'] = [6.0, 4.0]
    
    fig_home = raw.plot(events=None, duration=12.0, start=start_var, n_channels=20, bgcolor='w', color=None,
                        bad_color=(0.8, 0.8, 0.8), event_color='cyan', remove_dc=True, order=None,
                        show_options=True, title=None, show=False, block=False, highpass=None, lowpass=None,
                        filtorder=4, clipping=1.5, show_first_samp=False, proj=True, group_by='type',
                        butterfly=False,
                        decim='auto', noise_cov=None, event_id=None, show_scrollbars=True, show_scalebars=True,
                        verbose=None)

    # improve velocity
    # clipping = 1.5 default
    # clipping='transparent'
    
    # In Matplotlib, fig_home.axes[0].set_ylim is a method call that sets the
    # y - limits of the first(0 - indexed)subplot in a figure.
    # # fig_home.axes[0].set_ylim([-50, 50])
    
    # To ensure that the labels are displayed correctly, you can call fig_home.tight_layout()
    # at each figure  update, just before the canvas idget is created.This will ensure that the
    fig_home.tight_layout()
    
    # Create a canvas widget to display the figure
    canvas_widget = FigureCanvasTkAgg(fig_home, master=my_canvas)
    canvas_widget.draw()
    # Configure the canvas widget to expand and fill the canvas frame
    canvas_widget.get_tk_widget().place(relx=0, rely=0, relwidth=1, relheight=1)
    # canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)

    # Create a Frame inside my_canvas with the same size as my_canvas
    canvas_frame = tk.Frame(my_canvas, width=my_canvas.winfo_width(), height=my_canvas.winfo_height())
    my_canvas.create_window((0, 0), window=canvas_frame, anchor="nw")

    # Create a canvas widget to display the figure
    canvas_widget = FigureCanvasTkAgg(fig_home, master=my_canvas)
    canvas_widget.draw()

    # Place the canvas_widget inside the Frame
    canvas_widget.get_tk_widget().place(relx=0, rely=0, relwidth=1, relheight=1, in_=canvas_frame)

    # Bind a function to the <Configure> event of my_canvas to resize the Frame
    def resize_canvas_frame(event):
        canvas_frame.config(width=my_canvas.winfo_width(), height=my_canvas.winfo_height())

    my_canvas.bind("<Configure>", resize_canvas_frame)
1 Like