While using tutorial I get AttributeError: 'NoneType' object has no attribute '_get_renderer'

Good morning,

I’m relatively new to Python and completely new to EEG analysis so I’m working through the tutorial (specifically the Overview of MEG/EEG analysis). I’ve encountered some difficulty early in the process and I’m not sure how to fix it. I’ve searched the forum and found two posts that seemed to have the same error message (here and here) and both appear to have been solved up updating matplotlib to 3.8.0 which I have done. However, i’m still getting the same error.

  • MNE version: 1.5.4
    -matplotlib verion: 3.8
  • operating system: Windows 11
# MNE Tutorial file
# Import Necessary Modules
import numpy as np
import mne
import matplotlib


# continuing tutorial
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = (
    sample_data_folder / "MEG" / "sample" / "sample_audvis_filt-0-40_raw.fif"
)
raw2 = mne.io.read_raw_fif(sample_data_raw_file)

raw2.compute_psd(fmax=50).plot(picks="data", exclude="bads")

When I run that code, I get the following output in my console

raw2.compute_psd(fmax=50).plot(picks="data", exclude="bads")
Effective window size : 13.639 (s)
Traceback (most recent call last):

  File ~\AppData\Local\anaconda3\Lib\site-packages\IPython\core\formatters.py:340 in __call__
    return printer(obj)

  File ~\AppData\Local\anaconda3\Lib\site-packages\IPython\core\pylabtools.py:152 in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File ~\AppData\Roaming\Python\Python311\site-packages\matplotlib\backends\backend_qtagg.py:75 in print_figure
    super().print_figure(*args, **kwargs)

  File ~\AppData\Roaming\Python\Python311\site-packages\matplotlib\backend_bases.py:2174 in print_figure
    restore_bbox = _tight_bbox.adjust_bbox(

  File ~\AppData\Roaming\Python\Python311\site-packages\matplotlib\_tight_bbox.py:28 in adjust_bbox
    ax.apply_aspect(locator(ax, None))

  File ~\AppData\Local\anaconda3\Lib\site-packages\mpl_toolkits\axes_grid1\inset_locator.py:73 in __call__
    bbox = self.get_window_extent(renderer)

  File ~\AppData\Roaming\Python\Python311\site-packages\matplotlib\offsetbox.py:398 in get_window_extent
    renderer = self.figure._get_renderer()

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

Out[10]: <MNELineFigure size 1000x850 with 6 Axes>

The next line of code from the tutorial runs perfectly fine raw.plot(duration=5, n_channels=30) so I’m not sure what else to try.

Any help is greatly appreciated.

Hi @regnidem -

Can you run mne.sys_info() and share the output here?

Hello @scott-huberty

Thank you for the response. This is my output when I run that command (I think there was a typo in my original post, I have mne version 1.5.1)

mne.sys_info()
Platform             Windows-10-10.0.22621-SP0
Python               3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
Executable           C:\Users\medinger\AppData\Local\anaconda3\python.exe
CPU                  AMD64 Family 25 Model 68 Stepping 1, AuthenticAMD (16 cores)
Memory               30.8 GB

Core
├☑ mne               1.5.1
├☑ numpy             1.24.3 (MKL 2023.1-Product with 8 threads)
├☑ scipy             1.11.1
├☑ matplotlib        3.8.0 (backend=module://matplotlib_inline.backend_inline)
├☑ pooch             1.8.0
└☑ jinja2            3.1.2

Numerical (optional)
├☑ sklearn           1.3.0
├☑ numba             0.57.1
├☑ pandas            2.0.3
└☐ unavailable       nibabel, nilearn, dipy, openmeeg, cupy

Visualization (optional)
├☑ pyvista           0.42.3 (OpenGL 4.6.0 Compatibility Profile Context 22.20.44.50.230329 via AMD Radeon PRO Graphics)
├☑ vtk               9.2.6
├☑ qtpy              2.2.0 (PyQt5=5.15.2)
├☑ ipywidgets        8.0.4
└☐ unavailable       pyvistaqt, ipympl, pyqtgraph, mne-qt-browser, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
└☐ unavailable       mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline

I’ve just created a fresh environment with MNE 1.5.1 and I don’t replicate the error (though I’m on linux, not windows).

The traceback goes through ipython.pylabtools.print_figure and then a bunch of the matplotlib namespace, which makes me think either this is a specialized console or a notebook… can you say more about your execution environment?

(that said, it also works for me in an .ipynb cell run thorough VSCode or through jupyter notebook)

Hi @drammock

I appreciate the reply. That’s starting to get into areas of python that I don’t quite understand. I’ll do my best to describe what I did.

This is a fresh install of Anaconda using all the default settings. I launched Spyder 5.4.3 from the Anaconda Navigator. I installed MNE via pip install mne. I also used pip install numpy and pip install matplotlib to ensure I had the most recent versions of those packages as well.

I’m not sure if there is other information you need, or if there’s output from another command that will be helpful, please let me know and I’ll be happy to provide that as well.

that already helps, knowing that you’re within Spyder, and how you installed MNE. If you can, I would strongly recommend using our standalone installers rather than using pip to install MNE into your base anaconda environment.

In fact, generally speaking it is discouraged to install anything into your base anaconda environment. If you can’t / don’t want to use our installers, then I would suggest instead the following:

@drammock thanks for following up.

I uninstalled anaconda/python and then downloaded and installed via the standalone installer. This appears to have fixed the issue… kind of.

When I run raw.compute_psd(fmax=50).plot(picks="data", exclude="bads"), a plot appears in the Plots tab of Spyder. This is new. However, I still get an Attribute Error

Effective window size : 13.639 (s)
Traceback (most recent call last):

  File ~\AppData\Local\mne-python\1.5.1_0\Lib\site-packages\IPython\core\formatters.py:340 in __call__
    return printer(obj)

  File ~\AppData\Local\mne-python\1.5.1_0\Lib\site-packages\IPython\core\pylabtools.py:152 in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File ~\AppData\Local\mne-python\1.5.1_0\Lib\site-packages\matplotlib\backend_bases.py:2353 in print_figure
    restore_bbox = _tight_bbox.adjust_bbox(

  File ~\AppData\Local\mne-python\1.5.1_0\Lib\site-packages\matplotlib\_tight_bbox.py:28 in adjust_bbox
    ax.apply_aspect(locator(ax, None))

  File ~\AppData\Local\mne-python\1.5.1_0\Lib\site-packages\mpl_toolkits\axes_grid1\inset_locator.py:73 in __call__
    bbox = self.get_window_extent(renderer)

  File ~\AppData\Local\mne-python\1.5.1_0\Lib\site-packages\matplotlib\offsetbox.py:399 in get_window_extent
    renderer = self.figure._get_renderer()

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

Is there something else I need to do?

Also, this doesn’t cause a pop out window with the graph, but raw.plot(duration=5, n_channels=30) has a pop out, but no longer appears in the Plots tab. Do you know if this is expected behavior?

raw.plot() is a highly interactive figure, and by default it uses pyqtgraph instead of matplotlib as its backend plotting library (there is an option to force it to use matplotlib if needed). That is almost surely the reason why the raw.plot() call creates a separate window, and the spectrum.plot() call gets captured by Spyder and put into the “plots” pane (i.e., Spyder only intercepts matplotlib plots, not pyqtgraph plots).

As far as I can tell, this problem is a bug with Spyder and not with MNE-Python. I think you can work around it by going into Spyder preferences: Preferences > IPython Console > Graphics (tab) and either choosing Backend=Automatic, or if that doesn’t work, unticking “activate support”. That should make it so that matplotlib plots also pop open new windows.

One more question though: are you launching the version of spyder that was installed as part of the MNE standalone installer? Or did you separately install Spyder some other way? If you are using a separately-installed spyder there’s a chance that switching to the MNE-installed spyder might fix it.

2 Likes

I’m launching it using Spyder (MNE) when I search for apps so I assume I’m launching the version of Spyder that was included with the standalone installer.

I ran mne.sys_info() and noticed that it was using matplotlib 3.7.2 instead of 3.7.3 or 3.8, but since everything is kinda working, I’m afraid to even try and update it.

Sounds like you’re using the correct spyder version. I wouldn’t worry about updating matplotlib unless you know that you need features that were introduced in 3.8 (MNE plots don’t).

Was the Spyder preferences workaround successful, or do you still see the error? Are the plots popping up a new window now?

Sorry for the delay, I was out on vacation.

Both graphs are being plotted in a new window, which is fine. That was never really an issue for me; I was just wondering why it behaved that way.

That being said, I changed the python settings. I’m still getting the AttributeError: 'NoneType' object has no attribute '_get_renderer' error. But the plot is there, which I think is the point in the tutorial so I’m just going to shrug, say computers are stupid, and move on.