how to use matplotlib.pyplot to plot raw?

I get the following results using raw.plot()
raw

and then I use matplotlib.pyplot to draw the rawchannel=EEG C3-A2

plt

I check that the EEG F3-A2 curves of these two images are completely inconsistent, So how can I use matplotlib.pyplot to draw the same renderings as raw.plot() ,thank you

my code:

import mne
import matplotlib.pyplot as plt

raw = mne.io.read_raw_edf('../20220826/edfs/N91EDFS/210113.edf', preload=True)
raw.plot(start=60, duration=30, scalings=dict(eeg=1e-4, resp=1e3, eog=1e-4, emg=1e-7, misc=1e-1))


raw.drop_channels(['EEG ROC-A2', 'EEG LOC-A2', 'EEG F3-A2', 'EEG O1-A2', 'EMG Chin', 'ECG', 'Snoring Snore', 'Airflow', 'Resp Chest', 'Resp Abdomen', 'SaO2 SpO2', 'Pulse', 'Pos Body_Pos', 'Manual'])
data, times = raw[:, :]
print(data)
print(times)
plt.plot(times, data.T)
plt.title("EEG C3-A2")
plt.show()

Here is an example using the MNE-Python sample data. As you will see the data plotted are consistent, the difference is in the time range that is visible by default, and the scaling in the y-axis direction. If I force those to be equivalent the plots look quite similar.

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, preload=False)
raw.crop(tmax=20).load_data()

plt.ion()
picks = 'EEG 032'  # which channel to plot
scaling = 5e-4

# make figure sizes the same
fig, ax = plt.subplots(figsize=(24, 12))
mne.set_config('MNE_BROWSE_RAW_SIZE', '24,12')

# plot with matplotlib
ax.plot(raw.times, raw.get_data(picks=picks).squeeze())
ax.set(ylim=(-scaling, scaling))

# plot with MNE-Python
mne_fig = raw.pick(picks).plot(duration=raw.times[-1], scalings=dict(eeg=scaling))

Result:

2 Likes