Get rid of external white spaces from matplotlib plot

Viewed 40

I want to get rid of white spaces from this plot. The plot is from mne library example here. I only want to data in the box and nothing else. I converted the plot to image like this:

def get_img_from_fig(fig, dpi=180):
    buf = io.BytesIO()
    fig.savefig(buf, format="png", dpi=fig.dpi)
    buf.seek(0)
    img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8)
    buf.close()
    img = cv2.imdecode(img_arr, 1)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

enter image description here

I tried a lot of things including this:

plt.axis('off')
plt.imshow(img_orig_2)
plt.savefig('test.png', bbox_inches='tight',pad_inches = 0, dpi = 180)
plt.show()

but it won't get rid of the white space. What should I do, apart from manually cropping or setting the aspect ratio?

EDIT: Added the code to plot without axes and the link to the code for reference.

0 Answers
Related