Skip to content

BUG: Unexpected plot discrepancy when plotting as script compared to running in IPython #13956

@Gnefil

Description

@Gnefil

Description of the problem

Discrepancies between running figure plots as a Python script instead of running in IPython.
See fig1, fig2, fig3 as examples below.
This has been tested in a minimal plotting example including mne.viz.plot_topomap, stc.plot, mne.viz.plot_evoked_topo. In one case, topomap and stc plot are not synchronised when they should, in another case, the stc plot wouldn't even show up. All these happening when run as script, but works fine when run in IPython.

Steps to reproduce

# Working example when fig1 and fig2 plots. Synchronized. Both figures appear.

import matplotlib.pyplot as plt
import mne
data_path = mne.datasets.sample.data_path()

evoked_fname = data_path / "MEG/sample/sample_audvis-ave.fif"
evoked = mne.read_evokeds(evoked_fname, condition="Left Auditory").apply_baseline()

stc_fname = data_path / "MEG" / "sample" / "sample_audvis-meg-eeg"
stc = mne.read_source_estimate(stc_fname)


fig1 = evoked.plot_topomap("interactive")
fig2 = stc.plot("sample", subjects_dir=data_path / "subjects")
# fig3 = mne.viz.plot_evoked_topo(evoked)
link(fig1, fig2)

plt.show()


# Broken example, when run as a Python script. fig1 and fig2 no longer synchronised. All three figures appear.

import matplotlib.pyplot as plt
import mne
data_path = mne.datasets.sample.data_path()

evoked_fname = data_path / "MEG/sample/sample_audvis-ave.fif"
evoked = mne.read_evokeds(evoked_fname, condition="Left Auditory").apply_baseline()

stc_fname = data_path / "MEG" / "sample" / "sample_audvis-meg-eeg"
stc = mne.read_source_estimate(stc_fname)


fig1 = evoked.plot_topomap("interactive")
fig2 = stc.plot("sample", subjects_dir=data_path / "subjects")
fig3 = mne.viz.plot_evoked_topo(evoked)
link(fig1, fig2)


plt.show()


# Broken example, when run as a Python script. fig1 and fig2 no longer synchronised. Only fig1 and fig3 appear; fig2 won't appear.

import matplotlib.pyplot as plt
import mne
data_path = mne.datasets.sample.data_path()

evoked_fname = data_path / "MEG/sample/sample_audvis-ave.fif"
evoked = mne.read_evokeds(evoked_fname, condition="Left Auditory").apply_baseline()

stc_fname = data_path / "MEG" / "sample" / "sample_audvis-meg-eeg"
stc = mne.read_source_estimate(stc_fname)


fig1 = evoked.plot_topomap("interactive")
fig3 = mne.viz.plot_evoked_topo(evoked)
fig2 = stc.plot("sample", subjects_dir=data_path / "subjects")
link(fig1, fig2)


plt.show()


# Broken examples are run with `py ./location_to_script.py`. But when run with IPython, i.e. `ipython` command, followed by `%matplotlib qt` and `%run ./location_to_script.py`, the examples are not broken.

Link to data

No response

Expected results

The plots should behave properly, showing up and being synchronised even with fig3, just like running in IPython.

Actual results

Unexpected behaviour, as described in examples.

Additional information

Platform Windows-11-10.0.26200-SP0
Python 3.12.7 (tags/v3.12.7:0b05ead, Oct 1 2024, 03:06:41) [MSC v.1941 64 bit (AMD64)]
Executable D:\cloud\Projects\mne-python.venv\Scripts\python.exe
CPU Intel(R) Core(TM) i9-14900HX (32 cores)
Memory 63.6 GiB

Core

  • mne 1.12.0.dev128+gcdbdf9200 (unable to check for latest version on GitHub, timeout after 2.0 sec)
  • numpy 2.4.3 (OpenBLAS 0.3.31.dev with 24 threads)
  • scipy 1.17.1
  • matplotlib 3.10.8 (backend=qtagg)

Numerical (optional)

  • sklearn 1.8.0
  • nibabel 5.4.2
  • dipy 1.12.0
  • pandas 3.0.1
  • h5py 3.16.0
  • unavailable numba, nilearn, openmeeg, cupy, h5io

Visualization (optional)

  • pyvista 0.47.1 (OpenGL 4.5.0 - Build 32.0.101.6790 via Intel(R) RaptorLake-S Mobile Graphics Controller)
  • pyvistaqt 0.11.3
  • vtk 9.6.1
  • qtpy 2.4.3 (PySide6=6.11.0)
  • unavailable ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame_client, trame_server, trame_pyvista, trame_vtk, trame_vuetify

Ecosystem (optional)

  • mne-bids 0.18.0
  • mne-connectivity 0.8
  • neo 0.14.4
  • pymef 1.4.8
  • unavailable mne-nirs, mne-features, mne-icalabel, mne-bids-pipeline, eeglabio, edfio, curryreader, mffpy, pybv, antio, defusedxm

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions