2022-08-17

Matplotlib save very long plot in single image Python

I'm trying to save a very long EEG line plot (>100 hours with a datapoint every 5min) as a single image with a resolution ~10x10000px so that it would be readable by human eye when zoomed in.

I tried using figsize=(10000,10) but this just gives me an error that the image is too big. Anything in the figsize acceptable range returns a compressed image that is useless for EEG reading:

enter image description here

I also tried using MNE (https://mne.tools/stable/index.html) but I can only get it sight in an interactive window. A soon as I save it as a picture I get this compressed image of the full EEG: enter image description here

What I want instead is something like this: enter image description here

UPDATE:

I've tried both increasing the dpi and using .svg as format. This is the code as requested:

def visualize_eeg(mat_path):
    mat = scipy.io.loadmat(mat_path)
    fs = mat['fs'][0][0]
    ch_labels = [x[0].upper() for x in mat['ch_labels'][0]]
    eeg_data = list(list(x) for x in mat['eeg_data'])
    dict_data = dict(zip(ch_labels,eeg_data))
    df = pd.DataFrame(dict_data)
    df = df.T.sort_index().T
    fig = plt.figure(figsize=(50, 10)) 
    gs = gridspec.GridSpec(len(df.columns), 1,wspace=0.0, hspace=0.0)
    hours = get_hours(df,fs)
    # plot each column (channel) in a subplot
    for i in tqdm(range(len(df.columns))):
        ch = df.columns[i]
        ax = plt.subplot(gs[i])
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.set_yticks([0])
        ax.set_yticklabels([ch])
        if i == len(df.columns)-1:
            ax.tick_params(left = True, right = False , labelleft = True ,
                labelbottom = True, bottom = True)
            ax.set_xticks(hours.index,hours)
        else:
            ax.spines['bottom'].set_visible(False)
            ax.tick_params(left = True, right = False , labelleft = True ,
                    labelbottom = False, bottom = False)
        ax.plot(df[ch].apply(lambda x : x*1000), label=ch)
        
    fig.set_dpi(1000)
    fig.savefig('test.svg')

But what I get is just a scrollable crappy image (zoom or not): enter image description here enter image description here



No comments:

Post a Comment