Undo tight_layout

I am trying to find a way to “undo” or “reset” the tight_layout of a figure that has been created elsewhere.
I am running several plotting scripts that are part of a larger program, used by several people, so I cannot just comment out the line:

The minimal version of the code is:
def plot():
fig, ax = plt.subplots()
return fig, ax

Basically, I am looking for a function to do:
fig.normal_layout(), to reverse the changes that were done by tight_layout().

I tried fig.set_tight_layout(False), but this does not seem to work.


There is no automatic way to do this; tight_layout moves the axes, but it doesn’t try to remember where it moved them from. OTOH you could get the default axes position, and set your axes position to that (ax.set_position([l, b, w, h])). You can get the default position parameters from plt.rcParams['figure.subplot.left'] etc.

Curiosity compels me to ask why you would like to do this…

I understand why you are asking that. I will try to explain :slightly_smiling_face:

The figure is created using code that is part of software that is also used by others. It returns the figure, which I then slightly tweak in my own script (e.g. change the axis limits), before saving it. I used that figure in a paper. However, later, in the software, they decided to add tight_layout() to the code, which changes my figure. Now, I want to be able to reproduce that figure exactly how it was, so I was hoping there was a simple function I could use in my script to reverse the tight_layout after the fact.

I guess I can indeed change the positions of the axes again. However, I noticed that tight_layout() also changes the size of the axis labels and other text in my plot. Weirdly, printing out the label size e.g. with axes.xaxis.label.get_size(), gives me the same value before and after using tight_layout(), even though the size looks different…

Apologies for double posting my question. I was mainly asking the question about reversing the tight_layout here, and thought the outcome of get_tight_layout() was maybe a bug, so therefore I reported that one to the issues.

tight_layout doesn’t change any of the artists including fontsize so far as I know, except to reposition them. So maybe there are other changes in your upstream code?

Ideally the upstream code would have an old version saved somewhere that you could just use the old version? That may be easier than trying to reverse engineer where the axes used to be. Often a good thing for a paper is to save an environment with all your dependencies pinned so you can get back to the original version without pain.

Sorry I misread your GitHub issue! However @tacaswell’s answer was correct: tight_layout can be called on a figure at any time, whereas set_tight_layout(True) will further cause it to be called at draw time (without the user directly specifying it). However, in both cases the change is “irreversible” in that we don’t store a stack of old axes positions from before tight_layout was called. I’d be against adding that complication.