Obtaining bad channels and annotations from command line without closing plot window

I’m using MNE 0.20.7 with Python 3.7 on a Mac with OS 10.15.4.

Suppose I plot my raw data:


then remove bad channels and annotate in the plot window. I know that when I close the plot window,


stores the list of channels I de-selected and


stores the start times and durations of my annotations.

I’m curious to know if there’s a command line tool for obtaining this information from the window in its current state without having to close the window itself. This would be similar to retrieving axis information in matplotlib using a command such as ax.get_xticks()


I assume by “command-line tool” you mean some Python code?

raw.plot() returns a figure object which will have all the information you’re looking for. We’re actually making use of this in the MNE-BIDS Inspector:

So what you can do in your concrete example is:

fig = raw.plot()
# ... interact with the figure …

# retrieve current selection of bad channels and Annotations
# while the figure window is still open
bads = fig.mne.info['bads']
annotations = fig.mne.inst.annotations

This won’t work with block=True, as that will halt code execution until the window is closed.

I hope this helps.

Yes–by command line I meant Python code. Thanks for pointing out raw_fig–it’s exactly what I need. :grinning:

I’m trying to get this to work. Will I need to install mne-bids? I entered the syntax you suggested:

import mne

fig = raw.plot()
bads = fig.mne.info['bads']
annotations = fig.mne.inst.annotations

When I did this I encountered the following error message:

Traceback (most recent call last):
  File "/Users/fishbacp/Desktop/my_script.py", line 61, in <module>
    bads = fig.mne.info['bads']
AttributeError: 'Figure' object has no attribute 'mne'

I forgot to mention that the code example I posted relies on plotting functionality introduced with MNE 0.22. So once you update to this version, it should work :+1:

Got it! Updated to .22 and it works perfectly. Thanks so much. This forum is awesome!

1 Like

MNE .22 with Python 3.92 on a Mac with OS 10.15.4.

I wasn’t sure if I should start a new thread, but I’m also wondering if I can update raw itself based upon annotations I make when the plot window is open. I have a tkinter GUI in which the user plots, selects bad channels, and annotates. When they press a button, I want raw to be updated to reflect the removal of both bad channels and annotations. Your previous advice shows how to find the bad channels and remove them:


Now raw.info[‘ch_names’] will reflect an updated list of channels

Similarly, annotations I store as

annotations = fig.mne.inst.annotations

What is the simplest way to update raw to remove the annotations without having to access the starts and durations in the annotations?

Hi Paul,

Did you create this tkinter GUI yourself, or do you mean you are using the tkinter backend of matplotlib? By default raw.plot() uses whatever matplotlib GUI backend is currently set, and when you plot a raw object with fig = raw.plot() and then do some bad channel marking, annotation, etc., then when you close the figure window all of your annotations and bad channels will automatically be updated on the raw object in-place. So after the plot is closed, the original raw object should give you access to everything you want (if I’ve understood correctly what you want).

If you want to access the updated bads, annotations, etc while the plot is still open then Richard is correct that fig.mne.info['bads'] or fig.mne.inst.annotations is where those things are stored. It should be perfectly safe to read those values, however, note that the structure of fig.mne.* is fairly new, undocumented, and not yet guaranteed to be stable. In particular, we haven’t rigorously tested what happens if users edit the values in fig.mne.* while the plot is still open, so it’s possible that it might not behave the way you want it to.

If for your workflow you need fig.mne.* to be documented and stable and user-editable then please open a GitHub issue and tag @drammock on it and we can discuss further.


The GUI is one I’ve created myself. The idea is that the user can have both the GUI and the plot open in adjacent windows. Currently, when they mark bad channels in the plot and hit a button on my GUI, raw is updated to include only the good channels. The plot window stays open though. (This I accomplished with Richard’s help a week or so ago.)

I am aware of the “automatic updating” of raw you refer to when the window is closed, but for workflow purposes I’d like to use a button in my GUI to do it with the window open. So perhaps I should open the GitHub issue as you suggest.