_constrained_layout.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. """
  2. Adjust subplot layouts so that there are no overlapping axes or axes
  3. decorations. All axes decorations are dealt with (labels, ticks, titles,
  4. ticklabels) and some dependent artists are also dealt with (colorbar,
  5. suptitle).
  6. Layout is done via `~matplotlib.gridspec`, with one constraint per gridspec,
  7. so it is possible to have overlapping axes if the gridspecs overlap (i.e.
  8. using `~matplotlib.gridspec.GridSpecFromSubplotSpec`). Axes placed using
  9. ``figure.subplots()`` or ``figure.add_subplots()`` will participate in the
  10. layout. Axes manually placed via ``figure.add_axes()`` will not.
  11. See Tutorial: :ref:`constrainedlayout_guide`
  12. General idea:
  13. -------------
  14. First, a figure has a gridspec that divides the figure into nrows and ncols,
  15. with heights and widths set by ``height_ratios`` and ``width_ratios``,
  16. often just set to 1 for an equal grid.
  17. Subplotspecs that are derived from this gridspec can contain either a
  18. ``SubPanel``, a ``GridSpecFromSubplotSpec``, or an ``Axes``. The ``SubPanel``
  19. and ``GridSpecFromSubplotSpec`` are dealt with recursively and each contain an
  20. analogous layout.
  21. Each ``GridSpec`` has a ``_layoutgrid`` attached to it. The ``_layoutgrid``
  22. has the same logical layout as the ``GridSpec``. Each row of the grid spec
  23. has a top and bottom "margin" and each column has a left and right "margin".
  24. The "inner" height of each row is constrained to be the same (or as modified
  25. by ``height_ratio``), and the "inner" width of each column is
  26. constrained to be the same (as modified by ``width_ratio``), where "inner"
  27. is the width or height of each column/row minus the size of the margins.
  28. Then the size of the margins for each row and column are determined as the
  29. max width of the decorators on each axes that has decorators in that margin.
  30. For instance, a normal axes would have a left margin that includes the
  31. left ticklabels, and the ylabel if it exists. The right margin may include a
  32. colorbar, the bottom margin the xaxis decorations, and the top margin the
  33. title.
  34. With these constraints, the solver then finds appropriate bounds for the
  35. columns and rows. It's possible that the margins take up the whole figure,
  36. in which case the algorithm is not applied and a warning is raised.
  37. See the tutorial :ref:`constrainedlayout_guide`
  38. for more discussion of the algorithm with examples.
  39. """
  40. import logging
  41. import numpy as np
  42. from matplotlib import _api, artist as martist
  43. import matplotlib.transforms as mtransforms
  44. import matplotlib._layoutgrid as mlayoutgrid
  45. _log = logging.getLogger(__name__)
  46. ######################################################
  47. def do_constrained_layout(fig, h_pad, w_pad,
  48. hspace=None, wspace=None, rect=(0, 0, 1, 1),
  49. compress=False):
  50. """
  51. Do the constrained_layout. Called at draw time in
  52. ``figure.constrained_layout()``
  53. Parameters
  54. ----------
  55. fig : `~matplotlib.figure.Figure`
  56. `.Figure` instance to do the layout in.
  57. h_pad, w_pad : float
  58. Padding around the axes elements in figure-normalized units.
  59. hspace, wspace : float
  60. Fraction of the figure to dedicate to space between the
  61. axes. These are evenly spread between the gaps between the axes.
  62. A value of 0.2 for a three-column layout would have a space
  63. of 0.1 of the figure width between each column.
  64. If h/wspace < h/w_pad, then the pads are used instead.
  65. rect : tuple of 4 floats
  66. Rectangle in figure coordinates to perform constrained layout in
  67. [left, bottom, width, height], each from 0-1.
  68. compress : bool
  69. Whether to shift Axes so that white space in between them is
  70. removed. This is useful for simple grids of fixed-aspect Axes (e.g.
  71. a grid of images).
  72. Returns
  73. -------
  74. layoutgrid : private debugging structure
  75. """
  76. renderer = fig._get_renderer()
  77. # make layoutgrid tree...
  78. layoutgrids = make_layoutgrids(fig, None, rect=rect)
  79. if not layoutgrids['hasgrids']:
  80. _api.warn_external('There are no gridspecs with layoutgrids. '
  81. 'Possibly did not call parent GridSpec with the'
  82. ' "figure" keyword')
  83. return
  84. for _ in range(2):
  85. # do the algorithm twice. This has to be done because decorations
  86. # change size after the first re-position (i.e. x/yticklabels get
  87. # larger/smaller). This second reposition tends to be much milder,
  88. # so doing twice makes things work OK.
  89. # make margins for all the axes and subfigures in the
  90. # figure. Add margins for colorbars...
  91. make_layout_margins(layoutgrids, fig, renderer, h_pad=h_pad,
  92. w_pad=w_pad, hspace=hspace, wspace=wspace)
  93. make_margin_suptitles(layoutgrids, fig, renderer, h_pad=h_pad,
  94. w_pad=w_pad)
  95. # if a layout is such that a columns (or rows) margin has no
  96. # constraints, we need to make all such instances in the grid
  97. # match in margin size.
  98. match_submerged_margins(layoutgrids, fig)
  99. # update all the variables in the layout.
  100. layoutgrids[fig].update_variables()
  101. warn_collapsed = ('constrained_layout not applied because '
  102. 'axes sizes collapsed to zero. Try making '
  103. 'figure larger or axes decorations smaller.')
  104. if check_no_collapsed_axes(layoutgrids, fig):
  105. reposition_axes(layoutgrids, fig, renderer, h_pad=h_pad,
  106. w_pad=w_pad, hspace=hspace, wspace=wspace)
  107. if compress:
  108. layoutgrids = compress_fixed_aspect(layoutgrids, fig)
  109. layoutgrids[fig].update_variables()
  110. if check_no_collapsed_axes(layoutgrids, fig):
  111. reposition_axes(layoutgrids, fig, renderer, h_pad=h_pad,
  112. w_pad=w_pad, hspace=hspace, wspace=wspace)
  113. else:
  114. _api.warn_external(warn_collapsed)
  115. else:
  116. _api.warn_external(warn_collapsed)
  117. reset_margins(layoutgrids, fig)
  118. return layoutgrids
  119. def make_layoutgrids(fig, layoutgrids, rect=(0, 0, 1, 1)):
  120. """
  121. Make the layoutgrid tree.
  122. (Sub)Figures get a layoutgrid so we can have figure margins.
  123. Gridspecs that are attached to axes get a layoutgrid so axes
  124. can have margins.
  125. """
  126. if layoutgrids is None:
  127. layoutgrids = dict()
  128. layoutgrids['hasgrids'] = False
  129. if not hasattr(fig, '_parent'):
  130. # top figure; pass rect as parent to allow user-specified
  131. # margins
  132. layoutgrids[fig] = mlayoutgrid.LayoutGrid(parent=rect, name='figlb')
  133. else:
  134. # subfigure
  135. gs = fig._subplotspec.get_gridspec()
  136. # it is possible the gridspec containing this subfigure hasn't
  137. # been added to the tree yet:
  138. layoutgrids = make_layoutgrids_gs(layoutgrids, gs)
  139. # add the layoutgrid for the subfigure:
  140. parentlb = layoutgrids[gs]
  141. layoutgrids[fig] = mlayoutgrid.LayoutGrid(
  142. parent=parentlb,
  143. name='panellb',
  144. parent_inner=True,
  145. nrows=1, ncols=1,
  146. parent_pos=(fig._subplotspec.rowspan,
  147. fig._subplotspec.colspan))
  148. # recursively do all subfigures in this figure...
  149. for sfig in fig.subfigs:
  150. layoutgrids = make_layoutgrids(sfig, layoutgrids)
  151. # for each axes at the local level add its gridspec:
  152. for ax in fig._localaxes:
  153. gs = ax.get_gridspec()
  154. if gs is not None:
  155. layoutgrids = make_layoutgrids_gs(layoutgrids, gs)
  156. return layoutgrids
  157. def make_layoutgrids_gs(layoutgrids, gs):
  158. """
  159. Make the layoutgrid for a gridspec (and anything nested in the gridspec)
  160. """
  161. if gs in layoutgrids or gs.figure is None:
  162. return layoutgrids
  163. # in order to do constrained_layout there has to be at least *one*
  164. # gridspec in the tree:
  165. layoutgrids['hasgrids'] = True
  166. if not hasattr(gs, '_subplot_spec'):
  167. # normal gridspec
  168. parent = layoutgrids[gs.figure]
  169. layoutgrids[gs] = mlayoutgrid.LayoutGrid(
  170. parent=parent,
  171. parent_inner=True,
  172. name='gridspec',
  173. ncols=gs._ncols, nrows=gs._nrows,
  174. width_ratios=gs.get_width_ratios(),
  175. height_ratios=gs.get_height_ratios())
  176. else:
  177. # this is a gridspecfromsubplotspec:
  178. subplot_spec = gs._subplot_spec
  179. parentgs = subplot_spec.get_gridspec()
  180. # if a nested gridspec it is possible the parent is not in there yet:
  181. if parentgs not in layoutgrids:
  182. layoutgrids = make_layoutgrids_gs(layoutgrids, parentgs)
  183. subspeclb = layoutgrids[parentgs]
  184. # gridspecfromsubplotspec need an outer container:
  185. # get a unique representation:
  186. rep = (gs, 'top')
  187. if rep not in layoutgrids:
  188. layoutgrids[rep] = mlayoutgrid.LayoutGrid(
  189. parent=subspeclb,
  190. name='top',
  191. nrows=1, ncols=1,
  192. parent_pos=(subplot_spec.rowspan, subplot_spec.colspan))
  193. layoutgrids[gs] = mlayoutgrid.LayoutGrid(
  194. parent=layoutgrids[rep],
  195. name='gridspec',
  196. nrows=gs._nrows, ncols=gs._ncols,
  197. width_ratios=gs.get_width_ratios(),
  198. height_ratios=gs.get_height_ratios())
  199. return layoutgrids
  200. def check_no_collapsed_axes(layoutgrids, fig):
  201. """
  202. Check that no axes have collapsed to zero size.
  203. """
  204. for sfig in fig.subfigs:
  205. ok = check_no_collapsed_axes(layoutgrids, sfig)
  206. if not ok:
  207. return False
  208. for ax in fig.axes:
  209. gs = ax.get_gridspec()
  210. if gs in layoutgrids: # also implies gs is not None.
  211. lg = layoutgrids[gs]
  212. for i in range(gs.nrows):
  213. for j in range(gs.ncols):
  214. bb = lg.get_inner_bbox(i, j)
  215. if bb.width <= 0 or bb.height <= 0:
  216. return False
  217. return True
  218. def compress_fixed_aspect(layoutgrids, fig):
  219. gs = None
  220. for ax in fig.axes:
  221. if ax.get_subplotspec() is None:
  222. continue
  223. ax.apply_aspect()
  224. sub = ax.get_subplotspec()
  225. _gs = sub.get_gridspec()
  226. if gs is None:
  227. gs = _gs
  228. extraw = np.zeros(gs.ncols)
  229. extrah = np.zeros(gs.nrows)
  230. elif _gs != gs:
  231. raise ValueError('Cannot do compressed layout if axes are not'
  232. 'all from the same gridspec')
  233. orig = ax.get_position(original=True)
  234. actual = ax.get_position(original=False)
  235. dw = orig.width - actual.width
  236. if dw > 0:
  237. extraw[sub.colspan] = np.maximum(extraw[sub.colspan], dw)
  238. dh = orig.height - actual.height
  239. if dh > 0:
  240. extrah[sub.rowspan] = np.maximum(extrah[sub.rowspan], dh)
  241. if gs is None:
  242. raise ValueError('Cannot do compressed layout if no axes '
  243. 'are part of a gridspec.')
  244. w = np.sum(extraw) / 2
  245. layoutgrids[fig].edit_margin_min('left', w)
  246. layoutgrids[fig].edit_margin_min('right', w)
  247. h = np.sum(extrah) / 2
  248. layoutgrids[fig].edit_margin_min('top', h)
  249. layoutgrids[fig].edit_margin_min('bottom', h)
  250. return layoutgrids
  251. def get_margin_from_padding(obj, *, w_pad=0, h_pad=0,
  252. hspace=0, wspace=0):
  253. ss = obj._subplotspec
  254. gs = ss.get_gridspec()
  255. if hasattr(gs, 'hspace'):
  256. _hspace = (gs.hspace if gs.hspace is not None else hspace)
  257. _wspace = (gs.wspace if gs.wspace is not None else wspace)
  258. else:
  259. _hspace = (gs._hspace if gs._hspace is not None else hspace)
  260. _wspace = (gs._wspace if gs._wspace is not None else wspace)
  261. _wspace = _wspace / 2
  262. _hspace = _hspace / 2
  263. nrows, ncols = gs.get_geometry()
  264. # there are two margins for each direction. The "cb"
  265. # margins are for pads and colorbars, the non-"cb" are
  266. # for the axes decorations (labels etc).
  267. margin = {'leftcb': w_pad, 'rightcb': w_pad,
  268. 'bottomcb': h_pad, 'topcb': h_pad,
  269. 'left': 0, 'right': 0,
  270. 'top': 0, 'bottom': 0}
  271. if _wspace / ncols > w_pad:
  272. if ss.colspan.start > 0:
  273. margin['leftcb'] = _wspace / ncols
  274. if ss.colspan.stop < ncols:
  275. margin['rightcb'] = _wspace / ncols
  276. if _hspace / nrows > h_pad:
  277. if ss.rowspan.stop < nrows:
  278. margin['bottomcb'] = _hspace / nrows
  279. if ss.rowspan.start > 0:
  280. margin['topcb'] = _hspace / nrows
  281. return margin
  282. def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
  283. hspace=0, wspace=0):
  284. """
  285. For each axes, make a margin between the *pos* layoutbox and the
  286. *axes* layoutbox be a minimum size that can accommodate the
  287. decorations on the axis.
  288. Then make room for colorbars.
  289. Parameters
  290. ----------
  291. layoutgrids : dict
  292. fig : `~matplotlib.figure.Figure`
  293. `.Figure` instance to do the layout in.
  294. renderer : `~matplotlib.backend_bases.RendererBase` subclass.
  295. The renderer to use.
  296. w_pad, h_pad : float, default: 0
  297. Width and height padding (in fraction of figure).
  298. hspace, wspace : float, default: 0
  299. Width and height padding as fraction of figure size divided by
  300. number of columns or rows.
  301. """
  302. for sfig in fig.subfigs: # recursively make child panel margins
  303. ss = sfig._subplotspec
  304. gs = ss.get_gridspec()
  305. make_layout_margins(layoutgrids, sfig, renderer,
  306. w_pad=w_pad, h_pad=h_pad,
  307. hspace=hspace, wspace=wspace)
  308. margins = get_margin_from_padding(sfig, w_pad=0, h_pad=0,
  309. hspace=hspace, wspace=wspace)
  310. layoutgrids[gs].edit_outer_margin_mins(margins, ss)
  311. for ax in fig._localaxes:
  312. if not ax.get_subplotspec() or not ax.get_in_layout():
  313. continue
  314. ss = ax.get_subplotspec()
  315. gs = ss.get_gridspec()
  316. if gs not in layoutgrids:
  317. return
  318. margin = get_margin_from_padding(ax, w_pad=w_pad, h_pad=h_pad,
  319. hspace=hspace, wspace=wspace)
  320. pos, bbox = get_pos_and_bbox(ax, renderer)
  321. # the margin is the distance between the bounding box of the axes
  322. # and its position (plus the padding from above)
  323. margin['left'] += pos.x0 - bbox.x0
  324. margin['right'] += bbox.x1 - pos.x1
  325. # remember that rows are ordered from top:
  326. margin['bottom'] += pos.y0 - bbox.y0
  327. margin['top'] += bbox.y1 - pos.y1
  328. # make margin for colorbars. These margins go in the
  329. # padding margin, versus the margin for axes decorators.
  330. for cbax in ax._colorbars:
  331. # note pad is a fraction of the parent width...
  332. pad = colorbar_get_pad(layoutgrids, cbax)
  333. # colorbars can be child of more than one subplot spec:
  334. cbp_rspan, cbp_cspan = get_cb_parent_spans(cbax)
  335. loc = cbax._colorbar_info['location']
  336. cbpos, cbbbox = get_pos_and_bbox(cbax, renderer)
  337. if loc == 'right':
  338. if cbp_cspan.stop == ss.colspan.stop:
  339. # only increase if the colorbar is on the right edge
  340. margin['rightcb'] += cbbbox.width + pad
  341. elif loc == 'left':
  342. if cbp_cspan.start == ss.colspan.start:
  343. # only increase if the colorbar is on the left edge
  344. margin['leftcb'] += cbbbox.width + pad
  345. elif loc == 'top':
  346. if cbp_rspan.start == ss.rowspan.start:
  347. margin['topcb'] += cbbbox.height + pad
  348. else:
  349. if cbp_rspan.stop == ss.rowspan.stop:
  350. margin['bottomcb'] += cbbbox.height + pad
  351. # If the colorbars are wider than the parent box in the
  352. # cross direction
  353. if loc in ['top', 'bottom']:
  354. if (cbp_cspan.start == ss.colspan.start and
  355. cbbbox.x0 < bbox.x0):
  356. margin['left'] += bbox.x0 - cbbbox.x0
  357. if (cbp_cspan.stop == ss.colspan.stop and
  358. cbbbox.x1 > bbox.x1):
  359. margin['right'] += cbbbox.x1 - bbox.x1
  360. # or taller:
  361. if loc in ['left', 'right']:
  362. if (cbp_rspan.stop == ss.rowspan.stop and
  363. cbbbox.y0 < bbox.y0):
  364. margin['bottom'] += bbox.y0 - cbbbox.y0
  365. if (cbp_rspan.start == ss.rowspan.start and
  366. cbbbox.y1 > bbox.y1):
  367. margin['top'] += cbbbox.y1 - bbox.y1
  368. # pass the new margins down to the layout grid for the solution...
  369. layoutgrids[gs].edit_outer_margin_mins(margin, ss)
  370. # make margins for figure-level legends:
  371. for leg in fig.legends:
  372. inv_trans_fig = None
  373. if leg._outside_loc and leg._bbox_to_anchor is None:
  374. if inv_trans_fig is None:
  375. inv_trans_fig = fig.transFigure.inverted().transform_bbox
  376. bbox = inv_trans_fig(leg.get_tightbbox(renderer))
  377. w = bbox.width + 2 * w_pad
  378. h = bbox.height + 2 * h_pad
  379. legendloc = leg._outside_loc
  380. if legendloc == 'lower':
  381. layoutgrids[fig].edit_margin_min('bottom', h)
  382. elif legendloc == 'upper':
  383. layoutgrids[fig].edit_margin_min('top', h)
  384. if legendloc == 'right':
  385. layoutgrids[fig].edit_margin_min('right', w)
  386. elif legendloc == 'left':
  387. layoutgrids[fig].edit_margin_min('left', w)
  388. def make_margin_suptitles(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0):
  389. # Figure out how large the suptitle is and make the
  390. # top level figure margin larger.
  391. inv_trans_fig = fig.transFigure.inverted().transform_bbox
  392. # get the h_pad and w_pad as distances in the local subfigure coordinates:
  393. padbox = mtransforms.Bbox([[0, 0], [w_pad, h_pad]])
  394. padbox = (fig.transFigure -
  395. fig.transSubfigure).transform_bbox(padbox)
  396. h_pad_local = padbox.height
  397. w_pad_local = padbox.width
  398. for sfig in fig.subfigs:
  399. make_margin_suptitles(layoutgrids, sfig, renderer,
  400. w_pad=w_pad, h_pad=h_pad)
  401. if fig._suptitle is not None and fig._suptitle.get_in_layout():
  402. p = fig._suptitle.get_position()
  403. if getattr(fig._suptitle, '_autopos', False):
  404. fig._suptitle.set_position((p[0], 1 - h_pad_local))
  405. bbox = inv_trans_fig(fig._suptitle.get_tightbbox(renderer))
  406. layoutgrids[fig].edit_margin_min('top', bbox.height + 2 * h_pad)
  407. if fig._supxlabel is not None and fig._supxlabel.get_in_layout():
  408. p = fig._supxlabel.get_position()
  409. if getattr(fig._supxlabel, '_autopos', False):
  410. fig._supxlabel.set_position((p[0], h_pad_local))
  411. bbox = inv_trans_fig(fig._supxlabel.get_tightbbox(renderer))
  412. layoutgrids[fig].edit_margin_min('bottom',
  413. bbox.height + 2 * h_pad)
  414. if fig._supylabel is not None and fig._supylabel.get_in_layout():
  415. p = fig._supylabel.get_position()
  416. if getattr(fig._supylabel, '_autopos', False):
  417. fig._supylabel.set_position((w_pad_local, p[1]))
  418. bbox = inv_trans_fig(fig._supylabel.get_tightbbox(renderer))
  419. layoutgrids[fig].edit_margin_min('left', bbox.width + 2 * w_pad)
  420. def match_submerged_margins(layoutgrids, fig):
  421. """
  422. Make the margins that are submerged inside an Axes the same size.
  423. This allows axes that span two columns (or rows) that are offset
  424. from one another to have the same size.
  425. This gives the proper layout for something like::
  426. fig = plt.figure(constrained_layout=True)
  427. axs = fig.subplot_mosaic("AAAB\nCCDD")
  428. Without this routine, the axes D will be wider than C, because the
  429. margin width between the two columns in C has no width by default,
  430. whereas the margins between the two columns of D are set by the
  431. width of the margin between A and B. However, obviously the user would
  432. like C and D to be the same size, so we need to add constraints to these
  433. "submerged" margins.
  434. This routine makes all the interior margins the same, and the spacing
  435. between the three columns in A and the two column in C are all set to the
  436. margins between the two columns of D.
  437. See test_constrained_layout::test_constrained_layout12 for an example.
  438. """
  439. for sfig in fig.subfigs:
  440. match_submerged_margins(layoutgrids, sfig)
  441. axs = [a for a in fig.get_axes()
  442. if a.get_subplotspec() is not None and a.get_in_layout()]
  443. for ax1 in axs:
  444. ss1 = ax1.get_subplotspec()
  445. if ss1.get_gridspec() not in layoutgrids:
  446. axs.remove(ax1)
  447. continue
  448. lg1 = layoutgrids[ss1.get_gridspec()]
  449. # interior columns:
  450. if len(ss1.colspan) > 1:
  451. maxsubl = np.max(
  452. lg1.margin_vals['left'][ss1.colspan[1:]] +
  453. lg1.margin_vals['leftcb'][ss1.colspan[1:]]
  454. )
  455. maxsubr = np.max(
  456. lg1.margin_vals['right'][ss1.colspan[:-1]] +
  457. lg1.margin_vals['rightcb'][ss1.colspan[:-1]]
  458. )
  459. for ax2 in axs:
  460. ss2 = ax2.get_subplotspec()
  461. lg2 = layoutgrids[ss2.get_gridspec()]
  462. if lg2 is not None and len(ss2.colspan) > 1:
  463. maxsubl2 = np.max(
  464. lg2.margin_vals['left'][ss2.colspan[1:]] +
  465. lg2.margin_vals['leftcb'][ss2.colspan[1:]])
  466. if maxsubl2 > maxsubl:
  467. maxsubl = maxsubl2
  468. maxsubr2 = np.max(
  469. lg2.margin_vals['right'][ss2.colspan[:-1]] +
  470. lg2.margin_vals['rightcb'][ss2.colspan[:-1]])
  471. if maxsubr2 > maxsubr:
  472. maxsubr = maxsubr2
  473. for i in ss1.colspan[1:]:
  474. lg1.edit_margin_min('left', maxsubl, cell=i)
  475. for i in ss1.colspan[:-1]:
  476. lg1.edit_margin_min('right', maxsubr, cell=i)
  477. # interior rows:
  478. if len(ss1.rowspan) > 1:
  479. maxsubt = np.max(
  480. lg1.margin_vals['top'][ss1.rowspan[1:]] +
  481. lg1.margin_vals['topcb'][ss1.rowspan[1:]]
  482. )
  483. maxsubb = np.max(
  484. lg1.margin_vals['bottom'][ss1.rowspan[:-1]] +
  485. lg1.margin_vals['bottomcb'][ss1.rowspan[:-1]]
  486. )
  487. for ax2 in axs:
  488. ss2 = ax2.get_subplotspec()
  489. lg2 = layoutgrids[ss2.get_gridspec()]
  490. if lg2 is not None:
  491. if len(ss2.rowspan) > 1:
  492. maxsubt = np.max([np.max(
  493. lg2.margin_vals['top'][ss2.rowspan[1:]] +
  494. lg2.margin_vals['topcb'][ss2.rowspan[1:]]
  495. ), maxsubt])
  496. maxsubb = np.max([np.max(
  497. lg2.margin_vals['bottom'][ss2.rowspan[:-1]] +
  498. lg2.margin_vals['bottomcb'][ss2.rowspan[:-1]]
  499. ), maxsubb])
  500. for i in ss1.rowspan[1:]:
  501. lg1.edit_margin_min('top', maxsubt, cell=i)
  502. for i in ss1.rowspan[:-1]:
  503. lg1.edit_margin_min('bottom', maxsubb, cell=i)
  504. def get_cb_parent_spans(cbax):
  505. """
  506. Figure out which subplotspecs this colorbar belongs to.
  507. Parameters
  508. ----------
  509. cbax : `~matplotlib.axes.Axes`
  510. Axes for the colorbar.
  511. """
  512. rowstart = np.inf
  513. rowstop = -np.inf
  514. colstart = np.inf
  515. colstop = -np.inf
  516. for parent in cbax._colorbar_info['parents']:
  517. ss = parent.get_subplotspec()
  518. rowstart = min(ss.rowspan.start, rowstart)
  519. rowstop = max(ss.rowspan.stop, rowstop)
  520. colstart = min(ss.colspan.start, colstart)
  521. colstop = max(ss.colspan.stop, colstop)
  522. rowspan = range(rowstart, rowstop)
  523. colspan = range(colstart, colstop)
  524. return rowspan, colspan
  525. def get_pos_and_bbox(ax, renderer):
  526. """
  527. Get the position and the bbox for the axes.
  528. Parameters
  529. ----------
  530. ax : `~matplotlib.axes.Axes`
  531. renderer : `~matplotlib.backend_bases.RendererBase` subclass.
  532. Returns
  533. -------
  534. pos : `~matplotlib.transforms.Bbox`
  535. Position in figure coordinates.
  536. bbox : `~matplotlib.transforms.Bbox`
  537. Tight bounding box in figure coordinates.
  538. """
  539. fig = ax.figure
  540. pos = ax.get_position(original=True)
  541. # pos is in panel co-ords, but we need in figure for the layout
  542. pos = pos.transformed(fig.transSubfigure - fig.transFigure)
  543. tightbbox = martist._get_tightbbox_for_layout_only(ax, renderer)
  544. if tightbbox is None:
  545. bbox = pos
  546. else:
  547. bbox = tightbbox.transformed(fig.transFigure.inverted())
  548. return pos, bbox
  549. def reposition_axes(layoutgrids, fig, renderer, *,
  550. w_pad=0, h_pad=0, hspace=0, wspace=0):
  551. """
  552. Reposition all the axes based on the new inner bounding box.
  553. """
  554. trans_fig_to_subfig = fig.transFigure - fig.transSubfigure
  555. for sfig in fig.subfigs:
  556. bbox = layoutgrids[sfig].get_outer_bbox()
  557. sfig._redo_transform_rel_fig(
  558. bbox=bbox.transformed(trans_fig_to_subfig))
  559. reposition_axes(layoutgrids, sfig, renderer,
  560. w_pad=w_pad, h_pad=h_pad,
  561. wspace=wspace, hspace=hspace)
  562. for ax in fig._localaxes:
  563. if ax.get_subplotspec() is None or not ax.get_in_layout():
  564. continue
  565. # grid bbox is in Figure coordinates, but we specify in panel
  566. # coordinates...
  567. ss = ax.get_subplotspec()
  568. gs = ss.get_gridspec()
  569. if gs not in layoutgrids:
  570. return
  571. bbox = layoutgrids[gs].get_inner_bbox(rows=ss.rowspan,
  572. cols=ss.colspan)
  573. # transform from figure to panel for set_position:
  574. newbbox = trans_fig_to_subfig.transform_bbox(bbox)
  575. ax._set_position(newbbox)
  576. # move the colorbars:
  577. # we need to keep track of oldw and oldh if there is more than
  578. # one colorbar:
  579. offset = {'left': 0, 'right': 0, 'bottom': 0, 'top': 0}
  580. for nn, cbax in enumerate(ax._colorbars[::-1]):
  581. if ax == cbax._colorbar_info['parents'][0]:
  582. reposition_colorbar(layoutgrids, cbax, renderer,
  583. offset=offset)
  584. def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None):
  585. """
  586. Place the colorbar in its new place.
  587. Parameters
  588. ----------
  589. layoutgrids : dict
  590. cbax : `~matplotlib.axes.Axes`
  591. Axes for the colorbar.
  592. renderer : `~matplotlib.backend_bases.RendererBase` subclass.
  593. The renderer to use.
  594. offset : array-like
  595. Offset the colorbar needs to be pushed to in order to
  596. account for multiple colorbars.
  597. """
  598. parents = cbax._colorbar_info['parents']
  599. gs = parents[0].get_gridspec()
  600. fig = cbax.figure
  601. trans_fig_to_subfig = fig.transFigure - fig.transSubfigure
  602. cb_rspans, cb_cspans = get_cb_parent_spans(cbax)
  603. bboxparent = layoutgrids[gs].get_bbox_for_cb(rows=cb_rspans,
  604. cols=cb_cspans)
  605. pb = layoutgrids[gs].get_inner_bbox(rows=cb_rspans, cols=cb_cspans)
  606. location = cbax._colorbar_info['location']
  607. anchor = cbax._colorbar_info['anchor']
  608. fraction = cbax._colorbar_info['fraction']
  609. aspect = cbax._colorbar_info['aspect']
  610. shrink = cbax._colorbar_info['shrink']
  611. cbpos, cbbbox = get_pos_and_bbox(cbax, renderer)
  612. # Colorbar gets put at extreme edge of outer bbox of the subplotspec
  613. # It needs to be moved in by: 1) a pad 2) its "margin" 3) by
  614. # any colorbars already added at this location:
  615. cbpad = colorbar_get_pad(layoutgrids, cbax)
  616. if location in ('left', 'right'):
  617. # fraction and shrink are fractions of parent
  618. pbcb = pb.shrunk(fraction, shrink).anchored(anchor, pb)
  619. # The colorbar is at the left side of the parent. Need
  620. # to translate to right (or left)
  621. if location == 'right':
  622. lmargin = cbpos.x0 - cbbbox.x0
  623. dx = bboxparent.x1 - pbcb.x0 + offset['right']
  624. dx += cbpad + lmargin
  625. offset['right'] += cbbbox.width + cbpad
  626. pbcb = pbcb.translated(dx, 0)
  627. else:
  628. lmargin = cbpos.x0 - cbbbox.x0
  629. dx = bboxparent.x0 - pbcb.x0 # edge of parent
  630. dx += -cbbbox.width - cbpad + lmargin - offset['left']
  631. offset['left'] += cbbbox.width + cbpad
  632. pbcb = pbcb.translated(dx, 0)
  633. else: # horizontal axes:
  634. pbcb = pb.shrunk(shrink, fraction).anchored(anchor, pb)
  635. if location == 'top':
  636. bmargin = cbpos.y0 - cbbbox.y0
  637. dy = bboxparent.y1 - pbcb.y0 + offset['top']
  638. dy += cbpad + bmargin
  639. offset['top'] += cbbbox.height + cbpad
  640. pbcb = pbcb.translated(0, dy)
  641. else:
  642. bmargin = cbpos.y0 - cbbbox.y0
  643. dy = bboxparent.y0 - pbcb.y0
  644. dy += -cbbbox.height - cbpad + bmargin - offset['bottom']
  645. offset['bottom'] += cbbbox.height + cbpad
  646. pbcb = pbcb.translated(0, dy)
  647. pbcb = trans_fig_to_subfig.transform_bbox(pbcb)
  648. cbax.set_transform(fig.transSubfigure)
  649. cbax._set_position(pbcb)
  650. cbax.set_anchor(anchor)
  651. if location in ['bottom', 'top']:
  652. aspect = 1 / aspect
  653. cbax.set_box_aspect(aspect)
  654. cbax.set_aspect('auto')
  655. return offset
  656. def reset_margins(layoutgrids, fig):
  657. """
  658. Reset the margins in the layoutboxes of *fig*.
  659. Margins are usually set as a minimum, so if the figure gets smaller
  660. the minimum needs to be zero in order for it to grow again.
  661. """
  662. for sfig in fig.subfigs:
  663. reset_margins(layoutgrids, sfig)
  664. for ax in fig.axes:
  665. if ax.get_in_layout():
  666. gs = ax.get_gridspec()
  667. if gs in layoutgrids: # also implies gs is not None.
  668. layoutgrids[gs].reset_margins()
  669. layoutgrids[fig].reset_margins()
  670. def colorbar_get_pad(layoutgrids, cax):
  671. parents = cax._colorbar_info['parents']
  672. gs = parents[0].get_gridspec()
  673. cb_rspans, cb_cspans = get_cb_parent_spans(cax)
  674. bboxouter = layoutgrids[gs].get_inner_bbox(rows=cb_rspans, cols=cb_cspans)
  675. if cax._colorbar_info['location'] in ['right', 'left']:
  676. size = bboxouter.width
  677. else:
  678. size = bboxouter.height
  679. return cax._colorbar_info['pad'] * size