Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: set background color in raw.plot() #10317

Merged
merged 2 commits into from
Feb 11, 2022

Conversation

mmagnuski
Copy link
Member

@mmagnuski mmagnuski commented Feb 10, 2022

Reference issue

Example: Fixes #10314.

What does this implement/fix?

bgcolor argument was ignored by raw plotter.

import os.path as op
import mne

pth = op.join(mne.datasets.sample.data_path(), 'MEG', 'sample')
fname = 'sample_audvis_raw.fif'
raw = mne.io.read_raw(op.join(pth, fname))
raw.plot(bgcolor='red')

on main branch the bgcolor does not lead to any observable effects. Now it looks like this:

TODOs:

  • add test
  • whats new
  • check if other plotters need this change?

@agramfort
Copy link
Member

@mmagnuski can you share a code snippet to replicate the good behavior or provide a screenshot?

should you add a what's new entry?

@mmagnuski
Copy link
Member Author

What's new entry is in the todos , I will add it :)
I'm adding the code and a screenshot to the first post now.

@larsoner
Copy link
Member

Can you check to see if the pyqtgraph backend works? Just in your script do mne.viz.set_browse_backend('pyqtgraph') (after pip install mne-qt-browser)

@mmagnuski
Copy link
Member Author

@larsoner
Unfortunatelly I have problems running pyqtgraph backend"

mne.viz.set_browser_backend('pyqtgraph')
Traceback (most recent call last):

  File "C:\Users\mmagn\AppData\Local\Temp\ipykernel_16768\491008522.py", line 1, in <module>
    mne.viz.set_browser_backend('pyqtgraph')

  File "<decorator-gen-188>", line 24, in set_browser_backend

  File "C:\Users\mmagn\AppData\Roaming\Python\Python37\site-packages\mne\viz\_figure.py", line 684, in set_browser_backend
    _load_backend(backend_name)

  File "C:\Users\mmagn\AppData\Roaming\Python\Python37\site-packages\mne\viz\_figure.py", line 596, in _load_backend
    from mne_qt_browser import _pg_figure as backend

  File "C:\Users\mmagn\AppData\Roaming\Python\Python37\site-packages\mne_qt_browser\_pg_figure.py", line 20, in <module>
    from PyQt5.QtCore import (QEvent, QThread, Qt, pyqtSignal, QRectF, QLineF,

ImportError: DLL load failed: The specified procedure could not be found.

@larsoner larsoner merged commit 356dde9 into mne-tools:main Feb 11, 2022
@larsoner
Copy link
Member

Thanks @mmagnuski !

@mmagnuski
Copy link
Member Author

Thanks @larsoner - but I didn't write a test or fill whats new, should I still do it?

@larsoner
Copy link
Member

If you can think of an easy test, sure (maybe a .get_facecolor() somewhere would do it?). latest.inc would be good. Sorry I missed those...

@mmagnuski
Copy link
Member Author

(I can do it in another PR)

@mmagnuski
Copy link
Member Author

No problem :). Yes, I was thinking of a test like this. Ok, I can submit another PR in a few hours.

@fishbacp
Copy link

Am I correct that the errors stemmed from mne/viz/raw.py and mne/viz/_mpl_figure.py. I edited my local copies of these files to incorporate what I see on github:

image

(Please let me know if I should not have done that or whether I should do a module update instead.)

A new error resulting bgcolor arose in a different situation when I tried to plot my ica sources. The script throwing the error is again _mpl_figure.py. Should I describe that in this thread or post a new request? I had no problems plotting my ica sources prior to making the above minor edits.

@larsoner
Copy link
Member

Feel free to post a minimal example here

@fishbacp
Copy link

Here's my script, ICA_Source_Plot.py

import mne
from mne.preprocessing import ICA
import matplotlib
matplotlib.use('Qt5Agg',force=True)

raw=mne.io.read_raw_edf('sample_edf.edf', preload=True)
raw_temp=raw.copy().crop(tmin=0,tmax=240)
raw_filt = raw_temp.load_data().filter(l_freq=1., h_freq=None)
ica = ICA(method='fastica',n_components=5)
ica.fit(raw_filt,picks='all')
ica.plot_sources(raw_filt,show_scrollbars=True)

Here's the error message:

Traceback (most recent call last):
  File "/Users/fishbacp/Desktop/ICA_Source_Plot.py", line 13, in <module>
    ica.plot_sources(raw_filt,show_scrollbars=True)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/preprocessing/ica.py", line 1959, in plot_sources
    return plot_ica_sources(self, inst=inst, picks=picks,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/viz/ica.py", line 90, in plot_ica_sources
    fig = _plot_sources(ica, inst, picks, exclude, start=start, stop=stop,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/viz/ica.py", line 1099, in _plot_sources
    fig = _get_browser(**params)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/viz/_figure.py", line 613, in _get_browser
    browser = backend._init_browser(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/viz/_mpl_figure.py", line 2209, in _init_browser
    fig = _figure(toolbar=False, FigureClass=MNEBrowseFigure, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/viz/_mpl_figure.py", line 2050, in _figure
    fig = figure(FigureClass=FigureClass, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 768, in figure
    manager = new_figure_manager(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 316, in new_figure_manager
    return _backend_mod.new_figure_manager(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 3544, in new_figure_manager
    fig = fig_cls(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mne/viz/_mpl_figure.py", line 366, in __init__
    ax_main.set_facecolor(kwargs['bgcolor'])
KeyError: 'bgcolor'

@larsoner
Copy link
Member

@mmagnuski can you look?

@mmagnuski
Copy link
Member Author

@fishbacp
You must have an older version of the changes, currently bgcolor is set only if such key is present in kwargs.

@fishbacp
Copy link

@mmagnuski --does that mean I should not have edited mne/viz/raw.py and mne/viz/_mpl_figure.py as above to incorporate the changes highlighted in green? Should I just download the two versions of these scripts as located in https://github.com/mne-tools/mne-python/tree/main/mne/viz? You'll have to pardon my lack of experience using github.

@mmagnuski
Copy link
Member Author

Editing files by hand to follow changes in PRs can lead to errors so its better to pull changes from github via git. In your case you might have seen an unupdated view of the code changes (github shows an information at the top that the changes are not up to date, but it is easy to miss). The changes look like this:
image
notice the if statement added to avoid the error you report.

@mmagnuski
Copy link
Member Author

@fishbacp But I completely understand changing by hand BTW - to get the most up to date mne-python I'm afraid you would have to follow the mne contributing guide instructions and that requires installing git and installing mne in development version and it takes some time the first time you do it.

@fishbacp
Copy link

Thanks for the sound advice. It's good for me to learn these things, and I'll get started on doing so right away. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

raw.plot() does not produce specified background color.
4 participants