This is important because it seems so simple that it can't be so difficult.
The problem: I would like to create a plot that has a previously defined size that does not change regardless of the content of the plot and is retained when saving. The plot itself doesn't matter but I try to do a scatter plot right now so that's why I show this example:
import matplotlib.pyplot as plt
#create the plot
#yes the last points are outside the picture so there're lost
x = [279, 118, 350]
y = [302, 167, 550]
fig, ax = plt.subplots()
ax.set_ylim(ymin=0, ymax=528, auto=False)
ax.set_xlim(xmin=0, xmax=320, auto=False)
ax.axis('off')
plt.xlim(0, 320)
plt.ylim(0, 528)
plt.plot(x, y, 'o')
plt.subplots_adjust(left=0, right=1, top=1, bottom=0),
plt.show()
#save it
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('new2Dplot', format='tif', transparent=True, bbox_inches=extent)
The saved tif-file have the size of [496, 369] insteed of [320, 528] so it's also unusable. I think this problem have to do with margin, borders and the internal resize calculation, but why can't I just set everything to 0?
I think this posting is long enough so I won't write everything I tried. But things like: fig.tight_layout(), ax.get_tightbox(fig.canvas.get_renderer), bbox_inches='tight', of course I tried. My question seems very similar to this one Reduce left & right margins, but the solution they posted don't work for me.