如何使用Python在MNE中保持绘图窗口打开?

时间:2020-07-20 13:19:31

标签: python mne-python

在使用Python的MNE中,我希望在完全执行调用def后保持交互式绘图窗口打开。

但是,通过以下代码无法实现:

def get_plot():
    sample_data_folder = mne.datasets.sample.data_path()
    sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
                                        'sample_audvis_raw.fif')
    raw = mne.io.read_raw_fif(sample_data_raw_file)
    raw.plot()

get_plot()

get_plot()完成后,绘图窗口将自动关闭。

此外,我使用的是Windows 10 PyCharm 2020.1.3。

我可以知道该如何处理吗?

1 个答案:

答案 0 :(得分:0)

要在PyCharm中获得交互式剧情。首先需要禁用Show plots in tool window

禁用设置|工具| Python科学|在工具中显示图 窗口

然后,matplotlib.use('TkAgg')应该被允许创建一个interactive plot window

MNE plot()基于matplotlib。请参见源文件plot_raw。根据{{​​3}},matplotlib配备了块参数,您可以传递给plt.show()。这样即使在成功调用函数后也可以打开图。

显然,mne组也包含此参数。

因此,只需设置plot(block=True)

即可实现上述目标

然后,完整的代码是

import mne
import matplotlib
matplotlib.use('TkAgg')

def get_plot():
    sample_data_folder = mne.datasets.sample.data_path()
    sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
                                        'sample_audvis_raw.fif')
    raw = mne.io.read_raw_fif(sample_data_raw_file)
    raw.plot(block=True)

get_plot()