How to add mne plot to another subplot

Another alternative is to utilise the CanvasAgg backend directly to create image and extract this image to numpy array, which can in turn be passed off to matplotlib

First create the raw plot and expect an input in the form of MNEBrowseFigure

fig_raw=raw.plot(order=picks, show=False, n_channels=len(picks))

A canvas must be manually attached to the figure (pyplot would automatically do it). This is done by instantiating the canvas with the figure as argument.

canvas = FigureCanvasAgg(fig)

Retrieve a memory view on the renderer buffer, and convert it to a numpy array. There are two options to do this.

Option 1

canvas.draw ()
img = np.asarray(canvas.buffer_rgba())

Option 2

canvas.draw ()
img = np.asarray(canvas.buffer_rgba())
s, (width, height) = canvas.print_to_buffer ()
img = np.frombuffer ( s, np.uint8 ).reshape ( (height, width, 4) )

and pass it to matplotlib

sub1 = fig.add_subplot(2, 1, 2)
sub1.imshow(img)

This will output

Snap 2022-05-14 at 01.46.44

Reference
https://matplotlib.org/3.5.0/gallery/user_interfaces/canvasagg.html

Full code to reproduce the above figure

import os

import matplotlib.pyplot as plt
import mne
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

fig = plt.figure(figsize=(9, 10))

ax1 = fig.add_subplot(2, 1, 1)
ax1.plot(range(10))


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)
picks = mne.pick_channels_regexp(raw.ch_names, regexp='EEG 05.')
fig_raw=raw.plot(order=picks, show=False, n_channels=len(picks))

canvas = FigureCanvas (fig_raw)
canvas.draw ()

img = np.asarray(canvas.buffer_rgba())
sub1 = fig.add_subplot(2, 1, 2)
sub1.imshow(img)
plt.show()
1 Like