matplotlib has a function called tight_layout , which automatically adjusts subplot params so that the subplot(s) fits in to the figure area.
As stated in the docs, it's flagged as experimental, but is commonly used.
In my experience it should be called as late as possible (e.g. before using savefig) and probably after calls which change the geometry (like fig.set_size_inches
).
In the plt.show()
gui-window, one of the buttons is doing exactly this call too.
(converted to answer from earlier comment)