How to save plot_sensors_connectivity()?

plot_sensors_connectivity()

Hi,

I have a question about plot_sensors_connectivity().

Can I save the figure somehow or/and make subplots? I don’t need 3D figure, so I just need jpg or png picture.

Thanks!

The function returns a Matplotlib figure instance. So something like

from mne_connectivity.viz import plot_sensors_connectivity
fig = plot_sensors_connectivity(info, con, ...)
fig.set_size_inches(desired_width, desired_height)
fig.savefig('my_figure.png')

should work. If that doesn’t work, it’s a bug, please let us know.

Unfortunately it’s not working:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [26], in <module>
----> 1 fig.savefig('my_figure.png')

AttributeError: 'PyVistaFigure' object has no attribute 'savefig'

plot_sensors_connectivity() returns a 3D scene, so saving it to disk is a little bit more complicated. Here’s a fully reproducible working example:

# %%

import mne
from mne_connectivity import spectral_connectivity_epochs
from mne.datasets import sample
from mne_connectivity.viz import plot_sensors_connectivity
import matplotlib.pyplot as plt

data_path = sample.data_path()
raw_fname = data_path / 'MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path / 'MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'

raw = mne.io.read_raw_fif(raw_fname)
raw.info['bads'] += ['MEG 2443']
picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=True,
                       exclude='bads')

events = mne.read_events(event_fname)
event_id, tmin, tmax = 3, -0.2, 1.5  # need a long enough epoch for 5 cycles
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
                    baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6))

fmin, fmax = 4., 9.
sfreq = raw.info['sfreq']  # the sampling frequency
tmin = 0.0  # exclude the baseline period
epochs.load_data().pick_types(meg='grad')  # just keep MEG and no EOG now
con = spectral_connectivity_epochs(
    epochs, method='pli', mode='multitaper', sfreq=sfreq, fmin=fmin, fmax=fmax,
    faverage=True, tmin=tmin, mt_adaptive=False, n_jobs=1
)

# %%
# Do the actual plotting
connectivity_fig = plot_sensors_connectivity(
    epochs.info,
    con.get_data(output='dense')[:, :, 0]
)
# Take a screenshot of the 3D scene
screenshot = connectivity_fig.plotter.screenshot()

# The screenshot is just a NumPy array, so we can display it via imshow()
# and then save it to a file.
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(screenshot, origin='upper')
ax.set_axis_off()  # Disable axis labels and ticks
fig.tight_layout()
fig.savefig('connectivity.png', dpi=150)

3 Likes

@richard, thank you very much!

would it be possible to save the returned 3D scene as an interactive 3D pdf file?

I’m afraid this is currently not possible, sorry.!

Thank you for your reply.

Alternatively, it is possible to export the 3D scene as a glTF file (export_gltf — PyVista 0.37.0 documentation) and view it with an online viewer (https://gltf-viewer.donmccurdy.com/).

2 Likes

how did you do this? Because I have been having errors all day

@codZeus please open a new issue, and provide code for a minimal, reproducible example, and the error (including full traceback) you encounter when running that example in a fresh python session.

sure I will.

I asked a question about whitened evoked response and whitened GFP that hasn’t been answered. Do you mind helping me with that?

Thanks

there are lots of unanswered questions; I will get to it when I am able.