figure is shown, even if show= False when backend matplotlib when using VSCode Jupytet extension

I try to combine some figures (ica properties) into more complex group of figures, but I have problems with suppressing plot in jupyter notebooks even in case of raw objecyt, when browser_backend is set to matplotlib. Figure=False seems to be effective in Jupyter Lab (4.1.6) but not in Jupyter extension in Vscode (v2024.4.0).

import mne
import matplotlib.pyplot as plt

mne.viz.set_browser_backend("matplotlib")

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = sample_data_folder / "MEG" / "sample" / "sample_audvis_raw.fif"
# the preload flag loads the data into memory now
raw = mne.io.read_raw_fif(sample_data_raw_file, preload=True)
raw.crop(tmax=10.0)  # raw.crop() always happens in-place
fig_orig = raw.plot(show=False)
from mne.preprocessing import ICA

ica = ICA(n_components=2, max_iter="auto", random_state=97)
ica.fit(raw)
ica
ica_plot = ica.plot_properties(raw, show=False)
Platform             macOS-14.4.1-arm64-arm-64bit
Python               3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ]
Executable           [/Users/daniel/.pyenv/versions/conda-paths-3.12/bin/python](https://file+.vscode-resource.vscode-cdn.net/Users/daniel/.pyenv/versions/conda-paths-3.12/bin/python)
CPU                  arm (8 cores)
Memory               16.0 GB

Core
β”œβ˜‘ mne               1.7.0 (latest release)
β”œβ˜‘ numpy             1.26.4 (unknown linalg bindings)
β”œβ˜‘ scipy             1.13.0
β””β˜‘ matplotlib        3.8.4 (backend=module://matplotlib_inline.backend_inline)

Numerical (optional)
β”œβ˜‘ sklearn           1.4.2
β”œβ˜‘ numba             0.59.1
β”œβ˜‘ nibabel           5.2.1
β”œβ˜‘ nilearn           0.10.4
β”œβ˜‘ dipy              1.9.0
β”œβ˜‘ openmeeg          2.5.8
β”œβ˜‘ pandas            2.2.2
β”œβ˜‘ h5io              0.2.2
β”œβ˜‘ h5py              3.11.0
β””β˜ unavailable       cupy

Visualization (optional)
β”œβ˜‘ pyvista           0.43.5 (OpenGL 4.1 Metal - 88 via Apple M1)
β”œβ˜‘ pyvistaqt         0.11.0
β”œβ˜‘ vtk               9.2.6
β”œβ˜‘ qtpy              2.4.1 (PyQt5=5.15.8)
β”œβ˜‘ ipympl            0.9.4
β”œβ˜‘ pyqtgraph         0.13.5
β”œβ˜‘ mne-qt-browser    0.6.2
β”œβ˜‘ ipywidgets        8.1.2
β”œβ˜‘ trame_client      3.0.2
β”œβ˜‘ trame_server      3.0.0
β”œβ˜‘ trame_vtk         2.8.5
β””β˜‘ trame_vuetify     2.4.3

Ecosystem (optional)
β”œβ˜‘ mne-bids          0.14
β”œβ˜‘ mne-icalabel      0.6.0
β”œβ˜‘ mne-bids-pipeline 1.8.0
β”œβ˜‘ eeglabio          0.0.2-4
β”œβ˜‘ edfio             0.4.0
β”œβ˜‘ mffpy             0.8.0
β”œβ˜‘ pybv              0.7.5
β””β˜ unavailable       mne-nirs, mne-features, mne-connectivity, neo
1 Like

Hi, does ending the lines with the plot commands with a semicolon ; suppress the figure display?

Best wishes,
Richard

@richard thanks for the response!
both fig_orig = raw.plot(show=False); and fig_orig = raw.plot(show=False) lead to the same results (as you can see I assigning the output into fig_orig);

I don’t use ; , bc is removed by autoformatting by black :frowning: ( I turned off autoformatting , but I think this is not the the reason, maybe underlying rendering differences)

1 Like

@danieltomasz Thanks for testing this. I would consider this a bug. The MNE dev team had some (heated) discussions in the past about whether figures should be automatically shown or not. I would think that the issue you’re facing here adds a new piece of information that may lead to a re-evaluation of the current plotting behavior. I would appreciate if you could file a bug report on the GitHub issue tracker.

Richard

2 Likes

I will create an GitHub issue but first I need to investigate further; I noticed that show=False sometimes is ignored when using Jupyter Lab as well (there is no consistency, yesterday plot was suppressed in Jupyter Lab but not today with apparently the same kernel)

1 Like

I can confirm that this code snippet does not respect show=False in a jupyter interactive window or notebook in VSCode.

1 Like

it looks like possible upstream bug with jupyter/ipykernel/matplotlib(?);

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10)
plot = plt.plot(x, np.sin(x));

Will plot the plot (I am on Python 3.12.2), clean conda install using latest packages

I guess I hadn’t noticed that this occurs in VS code, but you can observe the same behavior in Colab notebooks:

Example