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

2024-04-20 11:32:39 发布

您现在位置: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()完成,绘图窗口将自动关闭

另外,我在Windows10上安装了PyCharm2020.1.3

我可以知道如何处理这个问题吗


Tags: samplepath代码绘图datagetrawplot
1条回答
网友
1楼 · 发布于 2024-04-20 11:32:39

以获取PyCharm中的交互式情节。首先需要禁用Show plots in tool window

Disable Settings | Tools | Python Scientific | Show plots in tool window

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

MNEplot()基于matplotlib。请参阅源文件plot_raw。根据OPmatplotlib配置块参数,可以传递给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()

相关问题 更多 >