Adding titles to PSD plots

  • MNE-Python version: 0.19.2 pypi (if necessary, I can update)
  • operating system: Windows 10

Hello,

I don’t find anything about the topic, so maybe my question is so pointless but I don’t know…

I’m wondering, how I can add a title to a plot?
I use the raw.plot_psd() function.

Thank you very much for your consideration!

You can use the ax parameter to pass an existing axis. Then, you can use matplotlib to add a title, e.g. something along these lines:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

# load raw data
raw.plot_psd(ax=ax)
ax.set_title("My title")

Hi,

Thank you very much for your reply.

I tried your code in this way but it does not work:

import matplotlib.pyplot as plt
import mne

#Load data
r = data_rest + “/AB24 rest_ipol_asr_ICA_MARA.set”

open the file in MNE
rest = mne.io.read_raw_eeglab(r, preload=False, uint16_codec=None, verbose=None)

#Your coding part
fig, ax = plt.subplots()

#using raw.plot_psd()
rest.plot_psd(ax=ax, fmin=0, fmax=50, tmin=0, tmax=420, proj=False, n_fft=1000,dB=True, n_overlap=500, reject_by_annotation=False)

ax.set_title(“Title”)

I also tried to use mne.viz.plot_raw_psd instead of raw.plot_psd:

mne.viz.plot_raw_psd(rest,ax=ax,fmin=0, fmax=50, tmin=0, tmax=420, proj=False, n_fft=1000,dB=True, n_overlap=500, reject_by_annotation=False)

Thank you very much!!
Angelika

What does that mean, specifically?

It plots a PSD properly, but it is not changing/adding the title.
There is no error popping up.

Thank you for asking specification :slight_smile:

Thanks, I could reproduce this now. I could make it work by passing show=False and showing the figure manually after updating the axis titles.

import os
import matplotlib.pyplot as plt
import mne

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
                                    'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False)


fig, ax = plt.subplots(3)  # In this example dataset, we have 3 channel types -> 3 axes required!
raw.plot_psd(ax=ax, show=False)

ax[0].set_title('foo')
ax[1].set_title('bar')
ax[2].set_title('baz')

fig.tight_layout()  # Without this, I got ugly overlaps between titles and plots!
fig.show()

@drammock Do you think it would make sense to add a titles kwarg to make this easier?

It works!!! Thank you very much!! :smiley:

1 Like