Plotting issues MNE 1.5.1, matplotlib backend choice?

Hi everyone,

It might be a messy explanation since I do not understand what exactly is going on, so please bear with me:

Plotting somehow works inconsistently for me, such that sometimes I can plot interactive pop-up windows and sometimes in the plot pane. This worked all right (playing around with matplotlib backends, even though I did not fully grasp what was happening under the hood) until recently.

For some reason, I am not able to plot as before, (pop-up windows open empty and show a loading sign for hours), and often the plot won’t even appear on the plotting pane, e.g.

 heartbeat_evoked_v1.plot()
Traceback (most recent call last):

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/IPython/core/formatters.py:340 in __call__
    return printer(obj)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/IPython/core/pylabtools.py:152 in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/backend_qtagg.py:75 in print_figure
    super().print_figure(*args, **kwargs)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backend_bases.py:2353 in print_figure
    restore_bbox = _tight_bbox.adjust_bbox(

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/_tight_bbox.py:28 in adjust_bbox
    ax.apply_aspect(locator(ax, None))

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/mpl_toolkits/axes_grid1/inset_locator.py:73 in __call__
    bbox = self.get_window_extent(renderer)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/offsetbox.py:399 in get_window_extent
    renderer = self.figure._get_renderer()

AttributeError: 'NoneType' object has no attribute '_get_renderer'

Out[20]: <Figure size 920x432 with 2 Axes>

or

heartbeat_evoked_v1.plot
Out[21]: <bound method Evoked.plot of <Evoked | '0.00 Γ— Comment/actiCAP Data On + 0.00 Γ— New Segment/ + 1.00 Γ— R-peak' (average, N=390), -0.3 – 0.8 s, baseline -0.3 – 0 s, 31 ch, ~110 kB>>

but no plot appearing

I figured I needed to update conda and MNE but hit a dead end because I could not update anything due to this error I was not able to circumvent even though I tried many recommended solutions:

(mne-1.5.1_0) denizyilmaz@Denizs-MacBook-Pro ~ % conda update anaconda
conda install spyder=5.5.1

Error while loading conda entry point: conda-libmamba-solver (dlopen(/Users/denizyilmaz/anaconda3/lib/python3.11/site-packages/libmambapy/bindings.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libarchive.19.dylib
  Referenced from: <9391E88D-A5AA-33C9-8306-75BC6FFBC365> /Users/denizyilmaz/anaconda3/lib/libmamba.2.0.0.dylib
  Reason: tried: '/Users/denizyilmaz/anaconda3/lib/libarchive.19.dylib' (no such file), '/Users/denizyilmaz/anaconda3/lib/python3.11/site-packages/libmambapy/../../../libarchive.19.dylib' (no such file), '/Users/denizyilmaz/anaconda3/lib/python3.11/site-packages/libmambapy/../../../libarchive.19.dylib' (no such file), '/Users/denizyilmaz/anaconda3/bin/../lib/libarchive.19.dylib' (no such file), '/Users/denizyilmaz/anaconda3/bin/../lib/libarchive.19.dylib' (no such file), '/usr/local/lib/libarchive.19.dylib' (no such file), '/usr/lib/libarchive.19.dylib' (no such file, not in dyld cache))

When I run the following command on my working session, where unexpectedly pop-up interactive plotting did work:

import mne
mne.sys_info() 

Platform             macOS-14.0-arm64-i386-64bit
Python               3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:33:12) [Clang 15.0.7 ]
Executable           /Applications/MNE-Python/1.5.1_0/.mne-python/bin/python
CPU                  i386 (10 cores)
Memory               16.0 GB

Core
β”œβ˜‘ mne               1.5.1
β”œβ˜‘ numpy             1.24.4 (OpenBLAS 0.3.24 with 10 threads)
β”œβ˜‘ scipy             1.11.2
β”œβ˜‘ matplotlib        3.7.2 (backend=TkAgg)
β”œβ˜‘ pooch             1.7.0
β””β˜‘ jinja2            3.1.2

Numerical (optional)
β”œβ˜‘ sklearn           1.3.0
β”œβ˜‘ numba             0.57.1
β”œβ˜‘ nibabel           5.0.1
β”œβ˜‘ nilearn           0.10.1
β”œβ˜‘ dipy              1.7.0
β”œβ˜‘ openmeeg          2.5.6
β”œβ˜‘ pandas            2.1.0
β””β˜ unavailable       cupy

Visualization (optional)
β”œβ˜‘ pyvista           0.41.1 (OpenGL 4.1 Metal - 86 via Apple M1 Pro)
β”œβ˜‘ pyvistaqt         0.0.0
β”œβ˜‘ vtk               9.2.6
β”œβ˜‘ qtpy              2.4.0 (PyQt5=5.15.8)
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/tkinter/__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 552, in destroy
    Gcf.destroy(self)
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/_pylab_helpers.py", line 66, in destroy
    manager.destroy()
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 587, in destroy
    delayed_destroy()
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 577, in delayed_destroy
    self.window.destroy()
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/tkinter/__init__.py", line 2368, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/tkinter/__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 552, in destroy
    Gcf.destroy(self)
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/_pylab_helpers.py", line 66, in destroy
    manager.destroy()
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 587, in destroy
    delayed_destroy()
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 577, in delayed_destroy
    self.window.destroy()
  File "/Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/tkinter/__init__.py", line 2368, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
2024-05-03 10:36:55,576 - OpenGL.acceleratesupport - INFO - No OpenGL_accelerate module loaded: No module named 'OpenGL_accelerate'
β”œβ˜‘ pyqtgraph         0.13.3
β”œβ˜‘ mne-qt-browser    0.5.2
β”œβ˜‘ ipywidgets        8.1.0
β”œβ˜‘ trame_client      2.11.2
β”œβ˜‘ trame_server      2.11.7
β”œβ˜‘ trame_vtk         2.5.8
β”œβ˜‘ trame_vuetify     2.3.1
β””β˜ unavailable       ipympl

Ecosystem (optional)
β”œβ˜‘ mne-bids          0.13
β”œβ˜‘ mne-nirs          0.5.0
β”œβ˜‘ mne-features      0.3
β”œβ˜‘ mne-connectivity  0.5.0
β”œβ˜‘ mne-icalabel      0.4
β””β˜‘ mne-bids-pipeline 1.4.0

Then again running it after restarting MNE, again I can only plot on the pane, cannot interact:

mne.sys_info()
Platform             macOS-14.0-arm64-i386-64bit
Python               3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:33:12) [Clang 15.0.7 ]
Executable           /Applications/MNE-Python/1.5.1_0/.mne-python/bin/python
CPU                  i386 (10 cores)
Memory               16.0 GB

Core
β”œβ˜‘ mne               1.5.1
β”œβ˜‘ numpy             1.24.4 (OpenBLAS 0.3.24 with 10 threads)
β”œβ˜‘ scipy             1.11.2
β”œβ˜‘ matplotlib        3.7.2 (backend=module://matplotlib_inline.backend_inline)
β”œβ˜‘ pooch             1.7.0
β””β˜‘ jinja2            3.1.2

Numerical (optional)
β”œβ˜‘ sklearn           1.3.0
β”œβ˜‘ numba             0.57.1
β”œβ˜‘ nibabel           5.0.1
β”œβ˜‘ nilearn           0.10.1
β”œβ˜‘ dipy              1.7.0
β”œβ˜‘ openmeeg          2.5.6
β”œβ˜‘ pandas            2.1.0
β””β˜ unavailable       cupy

Visualization (optional)
β”œβ˜‘ pyvista           0.41.1 (OpenGL 4.1 Metal - 86 via Apple M1 Pro)
β”œβ˜‘ pyvistaqt         0.0.0
β”œβ˜‘ vtk               9.2.6
β”œβ˜‘ qtpy              2.4.0 (PyQt5=5.15.8)
β”œβ˜‘ ipympl            0.9.3
β”œβ˜‘ pyqtgraph         0.13.3
β”œβ˜‘ mne-qt-browser    0.5.2
β”œβ˜‘ ipywidgets        8.1.0
β”œβ˜‘ trame_client      2.11.2
β”œβ˜‘ trame_server      2.11.7
β”œβ˜‘ trame_vtk         2.5.8
β””β˜‘ trame_vuetify     2.3.1

Ecosystem (optional)
β”œβ˜‘ mne-bids          0.13
β”œβ˜‘ mne-nirs          0.5.0
β”œβ˜‘ mne-features      0.3
β”œβ˜‘ mne-connectivity  0.5.0
β”œβ˜‘ mne-icalabel      0.4
β””β˜‘ mne-bids-pipeline 1.4.0

heartbeat_evoked_v1.plot()
Traceback (most recent call last):

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/IPython/core/formatters.py:340 in __call__
    return printer(obj)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/IPython/core/pylabtools.py:152 in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/backend_bases.py:2353 in print_figure
    restore_bbox = _tight_bbox.adjust_bbox(

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/_tight_bbox.py:28 in adjust_bbox
    ax.apply_aspect(locator(ax, None))

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/mpl_toolkits/axes_grid1/inset_locator.py:73 in __call__
    bbox = self.get_window_extent(renderer)

  File /Applications/MNE-Python/1.5.1_0/.mne-python/lib/python3.11/site-packages/matplotlib/offsetbox.py:399 in get_window_extent
    renderer = self.figure._get_renderer()

AttributeError: 'NoneType' object has no attribute '_get_renderer'

Out[11]: <Figure size 460.8x216 with 2 Axes>

In general, which backend would you recommend to stick to? I kept switching when one stopped working (plots not appearing at all, or only appearing on the console), but ideally, I would like to stick to one and ensure it keeps working…

I presume I might have used the standalone installer back then, but cannot remember certainly. I am using MNE Spyder.

I feel very stuck at this moment since all my attempts to fix it reached a dead end, thankful to hear your tips!

Best,
Deniz

Hello,

It seems you used the installer since the executable is in the path /Applications/MNE-Python.
I’m not sure how you ended in this unclean state, but the simplest is to wipe the installation and get a fresh one. So:

Mathieu

Hi Mathieu,

Thanks for the recommendation. I worry that the error with Anaconda persists and I cannot install anything for some reason (see the error regarding the libmamba).

Any ideas on how to fix the matplotlib backend issue and ensure I can always plot a pop-up figure interactively when I reinstall? Which backend should I prefer for this purpose?

Best
Deniz

I think you have a broken/unclean state which is both from MNE and Anaconda. With the procedure above, at least the MNE environment will start working again (since it it independent from your anaconda install); but indeed I would remove both Anaconda and the MNE-install.

Once installed with the installer, you should have in /Applications/MNE-Python a terminal with the label MNE, which is the terminal from which you can install additional package in the environment if you need.


For plotting with matplotlib, I prefer the QtAgg interactive backend. But anyway, it should automatically pick an interactive backend, except if you are in a jupyter notebook in which case you’ll need to add the magic command %matplotlib qt in one of the first cells to get interactive pop-up plots in matplotlib.

Mathieu

Thanks, Mathieu! I will try that out!
Last question: Do you think creating separate venvs in the MNE-env may circumvent such a complication in the future?

Separate virtual environment is the correct way to go, but only if you are comfortable enough with python, venvs, … the installer simplify this process by creating a separated virtual environment with a large number of useful package, and relevant shortcuts.

If you start mixing installer, with conda environment, with pip, you get a mess :wink:

Mathieu

2 Likes

Great!

I hope I am not missing an obvious answer but:
What are the general guidelines on installing additional packages, which are not included in the installer, if not conda or pip?

Best
Deniz

If you used the installer to create the environment, you should have in the created shortcut a Terminal (MNE) (or something similar) program which opens a terminal with the environment activated. The installer creates a conda environment, thus use conda in priority to install additional packages in this environment.

If like me you prefer virtual environment and pip, well conda is never part of it :wink:

Mathieu

1 Like