Tight layout working better with standard pyplot instead of gridspec?
Closed this issue · 3 comments
Hi,
The tight_layout
option in corner plots does not seem to always work perfectly. I sometimes still get small vertical white spaces between the subplots (see the first figure below). In this case, trying the suggested constrained_layout
option only introduces large horizontal white spaces between the subplots. However, all the white spaces seem to disappear (see the second figure below) if in this line
Line 1996 in d070091
changing
self.gridspec.tight_layout(self.fig, h_pad=self._share_kwargs.get('h_pad'), w_pad=self._share_kwargs.get('w_pad'), rect=rect)
to
plt.tight_layout(h_pad=self._share_kwargs.get('h_pad'), w_pad=self._share_kwargs.get('w_pad'), rect=rect)
.
Any idea why is that, and do you think changing to the latter would be ok?
The latter approach was actually used in some earlier versions of GetDist, so I am not sure if there are some reasons for not using it anymore. I also noticed that replacing self.fig
with plt.figure()
in the current line gets also rid of white spaces in the corner plot (printed in jupyter notebook), but then I get only blank images if exporting them.
Here are the figures produced using X-PSI interfaced with GetDist (with and without the white space):
Additional information: If I try to add tight layout outside of GetDist, using plt.tight_layout()
, I only get the following warning:
UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
, unless using pyplot instead of gridspec to set the tight_layout
in GetDist.
I guess the underlying is issue is that the "0.70" axis label just sticks out slightly, but it's tricky to prevent this happening in general. Is it fixed by decreasing the axis label font size slightly? Not sure why the gridspec tight layout would work differently here. Behaviour of constrained_layout has been varying a bit between matplotlib versions, but I think should ultimately be better.
I think you are right about the "0.70" label. I can indeed prevent this by decreasing the font size, or by rotating the tick labels a bit more. So this is indeed not a big problem. I was thinking that maybe grdispec working differently could be due to the self.fig
(given to gridspec) having the size "Figure(3600x3600)" but when saving the final figure, I get "Figure size 640x480 with 0 Axes".
Ps. Here is how the constrained_layout version looks like (using matplotlib version 3.5.2):