how to add a title to new psd function? raw.compute_psd().plot()

:question: If you have a question or issue with MNE-Python, please include the following info:

  • MNE version: 1.3.0
  • operating system: Windows 11

:page_facing_up: Please also provide relevant code snippets – ideally a minimal working example (MWE).
The following way definitely doesn’t works.
raw.compute_psd().plot(title=“rar”)
apparently the plot() fn returns a bool. Is there a way to easily add a title to the new compute_psd().plot() function?

HI @rmib200 welcome to the forum!

You can pass in a matplotlib.Axes object to the plot method of the Spectrum class. Then, you can set a custom title (among other things).

The bottom half of the code below should be what you are looking for.

import matplotlib.pyplot as plt
import mne
from mne.datasets.sample import data_path
# Just grabbing a sample file
fname = data_path() / "MEG" /"sample" / "sample_audvis_filt-0-40_raw.fif"
raw = mne.io.read_raw_fif(fname, preload=True)
raw.pick("eeg")
psd = raw.compute_psd()

# This is the relevant part
axes = plt.subplot() # create your own axes object
fig = psd.plot(axes=axes, show=False)
fig.axes[0].set_title("My PSD Plot") # access the axes object and set the title
plt.show()

Best,

Scott

4 Likes

And just a note for clarification

  • There isn’t a “title” parameter to the plot method, so passing a title parameter to the method will throw an error (it did for me).

  • the plot method doesn’t return a boolean but it returns a matplotlib.figure.Figure object.

2 Likes