Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72c80f4

Browse files
committed
FIX: Don't apply tight_layout if axes collapse
1 parent ab86e8f commit 72c80f4

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

lib/matplotlib/figure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2390,7 +2390,8 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
23902390
kwargs = get_tight_layout_figure(
23912391
self, self.axes, subplotspec_list, renderer,
23922392
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
2393-
self.subplots_adjust(**kwargs)
2393+
if kwargs:
2394+
self.subplots_adjust(**kwargs)
23942395

23952396
def align_xlabels(self, axs=None):
23962397
"""

lib/matplotlib/gridspec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ def tight_layout(self, figure, renderer=None,
341341
kwargs = tight_layout.get_tight_layout_figure(
342342
figure, figure.axes, subplotspec_list, renderer,
343343
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
344-
self.update(**kwargs)
344+
if kwargs:
345+
self.update(**kwargs)
345346

346347

347348
class GridSpecFromSubplotSpec(GridSpecBase):

lib/matplotlib/tight_layout.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ def auto_adjust_subplotpars(
172172
margin_bottom += pad_inches / fig_height_inch
173173

174174
if margin_left + margin_right >= 1:
175-
margin_left = 0.4999
176-
margin_right = 0.4999
177-
warnings.warn('The left and right margins cannot be made large '
175+
warnings.warn('Tight layout not applied. The left and right margins '
176+
'cannot be made large '
178177
'enough to accommodate all axes decorations. ')
178+
return None
179179
if margin_bottom + margin_top >= 1:
180-
margin_bottom = 0.4999
181-
margin_top = 0.4999
182-
warnings.warn('The bottom and top margins cannot be made large '
180+
warnings.warn('Tight layout not applied. '
181+
'The bottom and top margins cannot be made large '
183182
'enough to accommodate all axes decorations. ')
183+
return None
184184

185185
kwargs = dict(left=margin_left,
186186
right=1 - margin_right,
@@ -195,9 +195,10 @@ def auto_adjust_subplotpars(
195195
# axes widths:
196196
h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols
197197
if h_axes < 0:
198-
warnings.warn('tight_layout cannot make axes width small enough '
198+
warnings.warn('Tight layout not applied. '
199+
'tight_layout cannot make axes width small enough '
199200
'to accommodate all axes decorations')
200-
kwargs["wspace"] = 0.5
201+
return None
201202
else:
202203
kwargs["wspace"] = hspace / h_axes
203204

@@ -206,9 +207,10 @@ def auto_adjust_subplotpars(
206207
+ vpad_inches / fig_height_inch)
207208
v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows
208209
if v_axes < 0:
209-
warnings.warn('tight_layout cannot make axes height small enough '
210+
warnings.warn('Tight layout not applied. '
211+
'tight_layout cannot make axes height small enough '
210212
'to accommodate all axes decorations')
211-
kwargs["hspace"] = 0.5
213+
return None
212214
else:
213215
kwargs["hspace"] = vspace / v_axes
214216

0 commit comments

Comments
 (0)