0

I have to use the plot function from MNE library that returns a matplotlib figure of my plot.

As I'm builing an application under Tkinter, I want the figure to appear inside a window of my app.

I used this code. The plot is appearing both in the window of the app and in another matplotlib figure. The problem is that what appears in the app is static, it's just an instance of the matplotlib figure and I need an interactive one.


import tkinter as tk

from tkinter import ttk
from tkinter.filedialog import askopenfile

import matplotlib
matplotlib.use("TkAgg")

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

import mne

root = tk.Tk()

frame2 = tk.Frame(root)
frame2.pack(side= tk.LEFT,ipadx=500,ipady=350)



#Data path
file = 'C:\\Users\\...\chb01_01.edf'

#EDF to MNE Raw
data_raw = mne.io.read_raw_edf(file, preload=True)


but1 = tk.Button(frame2, text="plot",
                    command=lambda: time_plot(data_raw))
but1.pack()


def time_plot(data_raw):
    f = data_raw.plot(duration=5, n_channels=23)
    canvas = FigureCanvasTkAgg(f, frame2)

    toolbarFrame = tk.Frame(frame2)
    toolbarFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
    toolbar = NavigationToolbar2Tk(canvas, toolbarFrame)

    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)


root.mainloop()

Any idea on how to solve the problem?

imene.tar
  • 33
  • 5

1 Answers1

1

I came across your question while dealing with a similar situation. Here's how I plotted raw mne data on a canvas in a tkinter widget. Above the plot is a quit button.

import mne as mn
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pyplot as plt

root = tk.Tk()
root.wm_title("Embedding Raw Plot")

def _quit():
    root.quit()
    root.destroy()

button = tk.Button(master=root, text="Quit", command=_quit)
button.pack(side=tk.TOP)

raw = mn.io.read_raw_edf('sample.edf', preload=True)
fig=raw.plot(show=False,duration=30)


canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP)


tk.mainloop()
fishbacp
  • 1,123
  • 3
  • 14
  • 29