For some cases, the function ‘savefig’ with ‘bbox_inches=’tight” doesn’t work well and still generates images with borders that you want to remove.
plt.savefig('fn.jpg', dpi = 300, bbox_inches='tight')
One way to avoid this is to look into the axes properties before calling the function ‘savefig’. Basically, you just need to create an axes object without border/frame and add it to the pre-generated figure window.
def save_image(data, cm, fn): sizes = np.shape(data) height = float(sizes) width = float(sizes) fig = plt.figure() fig.set_size_inches(width/height, 1, forward=False) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) ax.imshow(data, cmap=cm) plt.savefig(fn, dpi = height) plt.close()