Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
Figure layout can change very slightly depending whethertight_layout()
is called once or twice. Another presumably equivalent way in which this can show up is withset_tight_layout(True)
: when saving a figure twice for which this has been called, both figures will slightly differ.
Code for reproduction
importmatplotlib.pyplotaspltfrommatplotlib.testing.compareimportcompare_images# import matplotlib as mpl# mpl.use("Agg") # issue also observed with Aggprint("using set_tight_layout(True):")fig,ax=plt.subplots()fig.set_tight_layout(True)fig.savefig("1.png")fig.savefig("2.png")fig.savefig("3.png")print(f" 1 and 2 match?{compare_images('1.png','2.png',0)isNone}")print(f" 2 and 3 match?{compare_images('2.png','3.png',0)isNone}")print("using tight_layout():")fig,ax=plt.subplots()fig.tight_layout()# fig.tight_layout() # when uncommenting this, 4/5/6 will matchfig.savefig("4.png")fig.tight_layout()# when commenting this out, 4.png and 5.png will matchfig.savefig("5.png")fig.tight_layout()fig.savefig("6.png")print(f" 4 and 5 match?{compare_images('4.png','5.png',0)isNone}")print(f" 5 and 6 match?{compare_images('5.png','6.png',0)isNone}")print("using constrained_layout=True:")fig,ax=plt.subplots(constrained_layout=True)fig.savefig("7.png")fig.savefig("8.png")fig.savefig("9.png")print(f" 7 and 8 match?{compare_images('7.png','8.png',0)isNone}")print(f" 8 and 9 match?{compare_images('8.png','9.png',0)isNone}")
Actual outcome
Output from running the example code:
using set_tight_layout(True): 1 and 2 match? False 2 and 3 match? Trueusing tight_layout(): 4 and 5 match? False 5 and 6 match? Trueusing constrained_layout=True: 7 and 8 match? True 8 and 9 match? True
When usingset_tight_layout(True)
, the first two figures being saved differ from each other. Subsequent figures look the same, the layout seems to have converged.
When callingtight_layout()
once before saving, and again before saving a second time, the resulting figures also differ. Calling it twice before saving for the first time results in consistent figures.
No such behavior is observed withconstrained_layout
in these tests.
Expected outcome
I would expect all produced figures withtight_layout
to be equivalent, similarly to how the layout withconstrained_layout
is stable.
Additional information
This was tested withmatplotlib
3.5.0, but also seems to happen in some earlier versions. 3.3.0 shows the same behavior. I do not know whether there are other versions where the tight layout converges in one step.
The differences between figures in this example are very small, more complicated figures can show slightly larger differences in layout, though in all cases I have seen, the differences tend to be very hard to see by eye unless skipping back and forth. I am not sure whether there are cases where layout convergence happens after more than two iterations, or where things oscillate between layouts.
I tested with MacOSX backend and a Retina display, though I see the same behavior in a container ofpython:3.9-slim
.
Switching to AGG as backend results in the same behavior.
This may be related to#21673. When adding afig.dpi = 100
after each figure is created, the behavior is also unchanged.
I've opened this as a bug but I am unsure whether it really is a bug, since the documentation fortight_layout
does not make any statement about guaranteeing convergence.
Operating system
macOS 11.5.2
Matplotlib Version
3.5.0
Matplotlib Backend
MacOSX
Python version
Python 3.8.10
Jupyter version
6.4.0
Installation
pip