I'm trying to find a way to convert a matplotlib figure to a numpy array without any white spaces or borders.
I found out that if I use the code below, I'll get something pretty close, but the numpy array "data" would still have some white borders. The white border (white changes as I scale the window) is saved as part of the numpy array but I only want the content.
Is there any way to get rid of this white area?

fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
ax.set_ylim(height, 0)
ax.set_xlim(0, width)
ax.axis('off')
fig.add_axes(ax)
ax.imshow(myimage) # Plus lots of other things
plt.show() # fig.canvas.draw() also works the same
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
plt.close()
A similar question has been asked here previously but the author's own work-around with PIL doesn't fit what I need.
figsizeargument ofplt.figure()to specify a figure size that has the same aspect ratio as your image. But this seems like an indirect method to accomplish what you want, and I don't know if it will perfectly eliminate the margins.