plot_compare_evokeds change of properties

Hi everyone,

I would like to change the font size on the ticks and the axis label (and other things appearence-wise) in the generated graph of the viz.plot_compare_evokeds function.

Can anyone give me a hint on how to do this? I can’t seem to figure it out.

Best wishes!

Hello,

You can provide show=False to disable drawing in plot_compare_evokeds, then change whatever properties of the matplotlib figure you want to change and show after with plt.show. You can provide the axes on which to draw the figure with the axes argument, giving you direct access to the matplotlib axes, or retrieve the figure returned by mne.viz.plot_compare_evokeds.

With the axes argument:

import mne
from matplotlib import pyplot as plt
from mne.io import read_raw_fif


# load raw data
folder = mne.datasets.sample.data_path() / "MEG" / "sample"
raw = read_raw_fif(folder / "sample_audvis_filt-0-40_raw.fif", preload=False)

# create epochs
events = mne.find_events(raw, stim_channel='STI 014')
event_dict = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3,
              'visual/right': 4, 'smiley': 5, 'buttonpress': 32}
epochs = mne.Epochs(raw, events, picks="eeg", event_id=event_dict, tmin=-0.2, 
                    tmax=0.5, reject=None, preload=True)
conds_we_care_about = ['auditory/left', 'auditory/right',
                       'visual/left', 'visual/right']
epochs.equalize_event_counts(conds_we_care_about)  # this operates in-place
aud_epochs = epochs['auditory']
vis_epochs = epochs['visual']
del raw, epochs  # free up memory

# create evoked
aud_evoked = aud_epochs.average()
vis_evoked = vis_epochs.average()

# plot
fig, axes = plt.subplots(1, 1, figsize=(10, 10))
mne.viz.plot_compare_evokeds(dict(auditory=aud_evoked, visual=vis_evoked),
                             legend='upper left', show_sensors='upper right',
                             axes=axes, show=False)
axes.set_xlabel("My X label")  # change properties
plt.show()

With the returned figure:

import mne
from matplotlib import pyplot as plt
from mne.io import read_raw_fif


# load raw data
folder = mne.datasets.sample.data_path() / "MEG" / "sample"
raw = read_raw_fif(folder / "sample_audvis_filt-0-40_raw.fif", preload=False)

# create epochs
events = mne.find_events(raw, stim_channel='STI 014')
event_dict = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3,
              'visual/right': 4, 'smiley': 5, 'buttonpress': 32}
epochs = mne.Epochs(raw, events, picks="eeg", event_id=event_dict, tmin=-0.2, 
                    tmax=0.5, reject=None, preload=True)
conds_we_care_about = ['auditory/left', 'auditory/right',
                       'visual/left', 'visual/right']
epochs.equalize_event_counts(conds_we_care_about)  # this operates in-place
aud_epochs = epochs['auditory']
vis_epochs = epochs['visual']
del raw, epochs  # free up memory

# create evoked
aud_evoked = aud_epochs.average()
vis_evoked = vis_epochs.average()

# plot
fig = mne.viz.plot_compare_evokeds(
    dict(auditory=aud_evoked, visual=vis_evoked),
    legend='upper left', show_sensors='upper right', show=False)
fig[0].axes[0].set_xlabel("My X label")  # change properties
plt.show()

Note that depending on your data and on the channel present, the number of axes/figures differs; which is why the function mne.viz.plot_compare_evokeds returns a list of figures.

3 Likes

Thank you @mscheltienne ! The second example is what I was looking for.

Can you by chance also tell me how to increase the size of the legend? :face_with_peeking_eye:

Not really, I am not super familiar with the matplotlib API and all the different settings and customization it proposes. I suggest looking at posts on Stack Overflow or on the matplotlib discourse forum, I’m sure you can find an answer.

2 Likes