Skip to content

Commit b6b8f10

Browse files
committed
FIX: simplify compress
1 parent 8175f87 commit b6b8f10

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -255,36 +255,41 @@ def check_no_collapsed_axes(layoutgrids, fig):
255255

256256

257257
def compress_fixed_aspect(layoutgrids, fig):
258-
extraw = dict()
259-
extrah = dict()
258+
gs = None
260259
for ax in fig.axes:
261260
if hasattr(ax, 'get_subplotspec'):
262261
actual = ax.get_position(original=False)
263262
ax.apply_aspect()
264263
sub = ax.get_subplotspec()
265-
gs = sub.get_gridspec()
266-
if gs not in extraw:
267-
extraw[gs] = np.zeros(gs.ncols)
268-
extrah[gs] = np.zeros(gs.nrows)
264+
_gs = sub.get_gridspec()
265+
if gs is None:
266+
gs = _gs
267+
extraw = np.zeros(gs.ncols)
268+
extrah = np.zeros(gs.nrows)
269+
elif _gs != gs:
270+
raise ValueError('Cannot do compressed layout if axes are not',
271+
'all from the same gridspec')
269272
orig = ax.get_position(original=True)
270273
actual = ax.get_position(original=False)
271274
dw = orig.width - actual.width
272275
if dw > 0:
273-
extraw[gs][sub.colspan] = np.maximum(extraw[gs][sub.colspan],
274-
dw)
276+
extraw[sub.colspan] = np.maximum(extraw[sub.colspan], dw)
275277
dh = orig.height - actual.height
276278
if dh > 0:
277-
extrah[gs][sub.rowspan] = np.maximum(extrah[gs][sub.rowspan],
278-
dh)
279+
extrah[sub.rowspan] = np.maximum(extrah[sub.rowspan], dh)
279280

280-
w = np.sum(extraw[gs]) / 2
281-
layoutgrids[fig].edit_margin_min('left', w)
282-
layoutgrids[fig].edit_margin_min('right', w)
281+
if gs is not None:
282+
w = np.sum(extraw) / 2
283+
layoutgrids[fig].edit_margin_min('left', w)
284+
layoutgrids[fig].edit_margin_min('right', w)
283285

284-
h = np.sum(extrah[gs]) / 2
285-
layoutgrids[fig].edit_margin_min('top', h)
286-
layoutgrids[fig].edit_margin_min('bottom', h)
287-
return layoutgrids
286+
h = np.sum(extrah) / 2
287+
layoutgrids[fig].edit_margin_min('top', h)
288+
layoutgrids[fig].edit_margin_min('bottom', h)
289+
return layoutgrids
290+
else:
291+
raise ValueError('Cannot do compressed layout if no axes ',
292+
'are part of a gridspec.')
288293

289294

290295
def get_margin_from_padding(obj, *, w_pad=0, h_pad=0,

0 commit comments

Comments
 (0)