Matplotlib Error: "figure includes Axes that are not compatible with tight_layout"
Asked Answered
L

2

39

After adding

bbox_inches="tight"

to an invocation of plt.savefig that has worked for several years, I get

/usr/local/lib/python2.7/site-packages/matplotlib/figure.py:1744: UserWarning:

This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect

The figure in question appears to work (without truncation of annotations now), but I wonder what this error could mean and whether there's anything obvious or known (without digging deep into complex figure code) that I can do to address it.

Is there some known or common cause for this that I should look for in my code?

Lasseter answered 14/2, 2016 at 16:4 Comment(2)
could you add the code you are using to generate the graph? I just had a look around it it appears to be an issues with adding additional axis or hidden axis linked to colorbarsAspic
You could have a look at Position colorbar inside figure thread which is about a similar issue faced with plotting colorbars along with subplots. This warning occurs if you have axes in your plots that aren't subplots. Eg: colorbar axis. The colorbar axis gets overlapped alongwith one of the subplots while rendering as a result.Supersonic
M
45

In my experience, plt.tight_layout doesn't always work but plt.savefig('fig.png',bbox_inches='tight') does. In addition, you don't need the former after using the latter and I have come to the conclusion after some pretty extensive testing of it.

Mattoid answered 22/8, 2016 at 22:27 Comment(0)
B
0
plt.rcParams['figure.constrained_layout.use'] = True

This worked for me.

Babble answered 21/6 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.