簡體   English   中英

如何使用 Python 在 MNE 中繼續繪制 window 打開?

[英]How to keep plotting window open in MNE with Python?

在具有 Python 的MNE中,一旦調用 def 完全執行,我想保持交互式繪圖 window 打開。

但是,這無法通過以下代碼實現:

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()完成, plot window 就會自動關閉。

另外,我在 Windows 10 和PyCharm 2020.1.3 上。

我可以知道如何處理這個問題嗎?

在 PyCharm 中獲取交互式 plot。 首先需要禁用Show plots in tool window

禁用設置 | 工具 | Python 科學 | 在工具 window 中顯示繪圖

然后, matplotlib.use('TkAgg')應該允許創建interactive plot window

MNE plot()基於matplotlib 請參閱源文件plot_raw 基於OPmatplotlib配備了可以傳遞給plt.show()的塊參數。 即使在成功調用 function 之后,這也允許 plot 打開。

顯然, 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM