Get rid of external white spaces from MNE tfr plot

MNE version 1.1.1
Google Colab

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

image
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()

I also went into the library to get the data but it still gives the plot with white spaces. What should I do besides manually cropping or setting the aspect ratio?

# this line is copied from the tutorial you linked to:
fig, = power.plot([82], baseline=(-0.5, 0), mode='logratio', title=power.ch_names[82])

# the rest is matplotlib commands for manipulating figure/axes elements
fig.suptitle('')  # remove the title
# you can print fig.axes to determine which is the image and which is the colorbar
fig.axes[1].remove()  # remove colorbar
fig.axes[0].set_axis_off()  # remove ticks, etc
fig.axes[0].set_position((0,0,1,1))  # remove margins
fig.savefig('whatever.png')
2 Likes

Thank you so much. This worked