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

Skip to content

Commit 491c388

Browse files
committed
FIX: tight_layout having negative width axes
1 parent c8fd779 commit 491c388

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

lib/matplotlib/tight_layout.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,40 @@ def auto_adjust_subplotpars(
171171
margin_bottom = max([sum(s) for s in vspaces[-cols:]] + [0])
172172
margin_bottom += pad_inches / fig_height_inch
173173

174+
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 '
178+
'enough to accomodate all axes decorations. ')
174179
kwargs = dict(left=margin_left,
175180
right=1 - margin_right,
176181
bottom=margin_bottom,
177182
top=1 - margin_top)
178-
179183
if cols > 1:
180184
hspace = (
181185
max(sum(s)
182186
for i in range(rows)
183187
for s in hspaces[i * (cols + 1) + 1:(i + 1) * (cols + 1) - 1])
184188
+ hpad_inches / fig_width_inch)
189+
# axes widths:
185190
h_axes = (1 - margin_right - margin_left - hspace * (cols - 1)) / cols
186-
kwargs["wspace"] = hspace / h_axes
191+
if h_axes < 0.:
192+
warnings.warn('tight_layout cannot make axes width small enough '
193+
'to accomodate all axes decorations')
194+
kwargs["wspace"] = 0.5
195+
else:
196+
kwargs["wspace"] = hspace / h_axes
187197

188198
if rows > 1:
189199
vspace = (max(sum(s) for s in vspaces[cols:-cols])
190200
+ vpad_inches / fig_height_inch)
191201
v_axes = (1 - margin_top - margin_bottom - vspace * (rows - 1)) / rows
192-
kwargs["hspace"] = vspace / v_axes
202+
if v_axes < 0:
203+
warnings.warn('tight_layout cannot make axes height small enough '
204+
'to accomodate all axes decorations')
205+
kwargs["hspace"] = 0.5
206+
else:
207+
kwargs["hspace"] = vspace / v_axes
193208

194209
return kwargs
195210

0 commit comments

Comments
 (0)