11
11
layout. Axes manually placed via ``figure.add_axes()`` will not.
12
12
13
13
See Tutorial: :doc:`/tutorials/intermediate/constrainedlayout_guide`
14
-
15
- General idea:
16
- -------------
17
-
18
- First, a figure has a gridspec that divides the figure into nrows and ncols,
19
- with heights and widths set by ``height_ratios`` and ``width_ratios``,
20
- often just set to 1 for an equal grid.
21
-
22
- Subplotspecs that are derived from this gridspec can contain either a
23
- ``SubPanel``, a ``GridSpecFromSubplotSpec``, or an ``Axes``. The ``SubPanel``
24
- and ``GridSpecFromSubplotSpec`` are dealt with recursively and each contain an
25
- analogous layout.
26
-
27
- Each ``GridSpec`` has a ``_layoutgrid`` attached to it. The ``_layoutgrid``
28
- has the same logical layout as the ``GridSpec``. Each row of the grid spec
29
- has a top and bottom "margin" and each column has a left and right "margin".
30
- The "inner" height of each row is constrained to be the same (or as modified
31
- by ``height_ratio``), and the "inner" width of each column is
32
- constrained to be the same (as modified by ``width_ratio``), where "inner"
33
- is the width or height of each column/row minus the size of the margins.
34
-
35
- Then the size of the margins for each row and column are determined as the
36
- max width of the decorators on each axes that has decorators in that margin.
37
- For instance, a normal axes would have a left margin that includes the
38
- left ticklabels, and the ylabel if it exists. The right margin may include a
39
- colorbar, the bottom margin the xaxis decorations, and the top margin the
40
- title.
41
-
42
- With these constraints, the solver then finds appropriate bounds for the
43
- columns and rows. It's possible that the margins take up the whole figure,
44
- in which case the algorithm is not applied and a warning is raised.
45
-
46
- See the tutorial doc:`/tutorials/intermediate/constrainedlayout_guide`
47
- for more discussion of the algorithm with examples.
48
14
"""
49
15
50
16
import logging
51
17
52
18
import numpy as np
53
19
54
20
from matplotlib import _api , artist as martist
55
- from matplotlib ._tight_layout import get_renderer
56
21
import matplotlib .transforms as mtransforms
57
22
import matplotlib ._layoutgrid as mlayoutgrid
58
23
59
24
25
+ # General idea:
26
+ # -------------
27
+ #
28
+ # First, a figure has a gridspec that divides the figure into nrows and ncols,
29
+ # with heights and widths set by ``height_ratios`` and ``width_ratios``,
30
+ # often just set to 1 for an equal grid.
31
+ #
32
+ # Subplotspecs that are derived from this gridspec can contain either a
33
+ # ``SubPanel``, a ``GridSpecFromSubplotSpec``, or an ``Axes``. The ``SubPanel``
34
+ # and ``GridSpecFromSubplotSpec`` are dealt with recursively and each contain an
35
+ # analogous layout.
36
+ #
37
+ # Each ``GridSpec`` has a ``_layoutgrid`` attached to it. The ``_layoutgrid``
38
+ # has the same logical layout as the ``GridSpec``. Each row of the grid spec
39
+ # has a top and bottom "margin" and each column has a left and right "margin".
40
+ # The "inner" height of each row is constrained to be the same (or as modified
41
+ # by ``height_ratio``), and the "inner" width of each column is
42
+ # constrained to be the same (as modified by ``width_ratio``), where "inner"
43
+ # is the width or height of each column/row minus the size of the margins.
44
+ #
45
+ # Then the size of the margins for each row and column are determined as the
46
+ # max width of the decorators on each axes that has decorators in that margin.
47
+ # For instance, a normal axes would have a left margin that includes the
48
+ # left ticklabels, and the ylabel if it exists. The right margin may include a
49
+ # colorbar, the bottom margin the xaxis decorations, and the top margin the
50
+ # title.
51
+ #
52
+ # With these constraints, the solver then finds appropriate bounds for the
53
+ # columns and rows. It's possible that the margins take up the whole figure,
54
+ # in which case the algorithm is not applied and a warning is raised.
55
+ #
56
+ # See the tutorial doc:`/tutorials/intermediate/constrainedlayout_guide`
57
+ # for more discussion of the algorithm with examples.
58
+
60
59
_log = logging .getLogger (__name__ )
61
60
62
61
@@ -72,8 +71,6 @@ def do_constrained_layout(fig, h_pad, w_pad,
72
71
fig : Figure
73
72
``Figure`` instance to do the layout in.
74
73
75
- renderer : Renderer
76
- Renderer to use.
77
74
78
75
h_pad, w_pad : float
79
76
Padding around the axes elements in figure-normalized units.
@@ -94,7 +91,6 @@ def do_constrained_layout(fig, h_pad, w_pad,
94
91
layoutgrid : private debugging structure
95
92
"""
96
93
97
- renderer = get_renderer (fig )
98
94
# make layoutgrid tree...
99
95
layoutgrids = make_layoutgrids (fig , None , rect = rect )
100
96
if not layoutgrids ['hasgrids' ]:
@@ -111,9 +107,9 @@ def do_constrained_layout(fig, h_pad, w_pad,
111
107
112
108
# make margins for all the axes and subfigures in the
113
109
# figure. Add margins for colorbars...
114
- make_layout_margins (layoutgrids , fig , renderer , h_pad = h_pad ,
110
+ make_layout_margins (layoutgrids , fig , h_pad = h_pad ,
115
111
w_pad = w_pad , hspace = hspace , wspace = wspace )
116
- make_margin_suptitles (layoutgrids , fig , renderer , h_pad = h_pad ,
112
+ make_margin_suptitles (layoutgrids , fig , h_pad = h_pad ,
117
113
w_pad = w_pad )
118
114
119
115
# if a layout is such that a columns (or rows) margin has no
@@ -125,7 +121,7 @@ def do_constrained_layout(fig, h_pad, w_pad,
125
121
layoutgrids [fig ].update_variables ()
126
122
127
123
if check_no_collapsed_axes (layoutgrids , fig ):
128
- reposition_axes (layoutgrids , fig , renderer , h_pad = h_pad ,
124
+ reposition_axes (layoutgrids , fig , h_pad = h_pad ,
129
125
w_pad = w_pad , hspace = hspace , wspace = wspace )
130
126
else :
131
127
_api .warn_external ('constrained_layout not applied because '
@@ -286,7 +282,7 @@ def get_margin_from_padding(obj, *, w_pad=0, h_pad=0,
286
282
return margin
287
283
288
284
289
- def make_layout_margins (layoutgrids , fig , renderer , * , w_pad = 0 , h_pad = 0 ,
285
+ def make_layout_margins (layoutgrids , fig , * , w_pad = 0 , h_pad = 0 ,
290
286
hspace = 0 , wspace = 0 ):
291
287
"""
292
288
For each axes, make a margin between the *pos* layoutbox and the
@@ -297,7 +293,7 @@ def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
297
293
"""
298
294
for sfig in fig .subfigs : # recursively make child panel margins
299
295
ss = sfig ._subplotspec
300
- make_layout_margins (layoutgrids , sfig , renderer ,
296
+ make_layout_margins (layoutgrids , sfig ,
301
297
w_pad = w_pad , h_pad = h_pad ,
302
298
hspace = hspace , wspace = wspace )
303
299
@@ -317,7 +313,7 @@ def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
317
313
318
314
margin = get_margin_from_padding (ax , w_pad = w_pad , h_pad = h_pad ,
319
315
hspace = hspace , wspace = wspace )
320
- pos , bbox = get_pos_and_bbox (ax , renderer )
316
+ pos , bbox = get_pos_and_bbox (ax )
321
317
# the margin is the distance between the bounding box of the axes
322
318
# and its position (plus the padding from above)
323
319
margin ['left' ] += pos .x0 - bbox .x0
@@ -334,7 +330,7 @@ def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
334
330
# colorbars can be child of more than one subplot spec:
335
331
cbp_rspan , cbp_cspan = get_cb_parent_spans (cbax )
336
332
loc = cbax ._colorbar_info ['location' ]
337
- cbpos , cbbbox = get_pos_and_bbox (cbax , renderer )
333
+ cbpos , cbbbox = get_pos_and_bbox (cbax )
338
334
if loc == 'right' :
339
335
if cbp_cspan .stop == ss .colspan .stop :
340
336
# only increase if the colorbar is on the right edge
@@ -370,7 +366,7 @@ def make_layout_margins(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0,
370
366
layoutgrids [gs ].edit_outer_margin_mins (margin , ss )
371
367
372
368
373
- def make_margin_suptitles (layoutgrids , fig , renderer , * , w_pad = 0 , h_pad = 0 ):
369
+ def make_margin_suptitles (layoutgrids , fig , * , w_pad = 0 , h_pad = 0 ):
374
370
# Figure out how large the suptitle is and make the
375
371
# top level figure margin larger.
376
372
@@ -383,29 +379,29 @@ def make_margin_suptitles(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0):
383
379
w_pad_local = padbox .width
384
380
385
381
for sfig in fig .subfigs :
386
- make_margin_suptitles (layoutgrids , sfig , renderer ,
382
+ make_margin_suptitles (layoutgrids , sfig ,
387
383
w_pad = w_pad , h_pad = h_pad )
388
384
389
385
if fig ._suptitle is not None and fig ._suptitle .get_in_layout ():
390
386
p = fig ._suptitle .get_position ()
391
387
if getattr (fig ._suptitle , '_autopos' , False ):
392
388
fig ._suptitle .set_position ((p [0 ], 1 - h_pad_local ))
393
- bbox = inv_trans_fig (fig ._suptitle .get_tightbbox (renderer ))
389
+ bbox = inv_trans_fig (fig ._suptitle .get_tightbbox ())
394
390
layoutgrids [fig ].edit_margin_min ('top' , bbox .height + 2 * h_pad )
395
391
396
392
if fig ._supxlabel is not None and fig ._supxlabel .get_in_layout ():
397
393
p = fig ._supxlabel .get_position ()
398
394
if getattr (fig ._supxlabel , '_autopos' , False ):
399
395
fig ._supxlabel .set_position ((p [0 ], h_pad_local ))
400
- bbox = inv_trans_fig (fig ._supxlabel .get_tightbbox (renderer ))
396
+ bbox = inv_trans_fig (fig ._supxlabel .get_tightbbox ())
401
397
layoutgrids [fig ].edit_margin_min ('bottom' ,
402
398
bbox .height + 2 * h_pad )
403
399
404
400
if fig ._supylabel is not None and fig ._supylabel .get_in_layout ():
405
401
p = fig ._supylabel .get_position ()
406
402
if getattr (fig ._supylabel , '_autopos' , False ):
407
403
fig ._supylabel .set_position ((w_pad_local , p [1 ]))
408
- bbox = inv_trans_fig (fig ._supylabel .get_tightbbox (renderer ))
404
+ bbox = inv_trans_fig (fig ._supylabel .get_tightbbox ())
409
405
layoutgrids [fig ].edit_margin_min ('left' , bbox .width + 2 * w_pad )
410
406
411
407
@@ -526,14 +522,14 @@ def get_cb_parent_spans(cbax):
526
522
return rowspan , colspan
527
523
528
524
529
- def get_pos_and_bbox (ax , renderer ):
525
+ def get_pos_and_bbox (ax ):
530
526
"""
531
527
Get the position and the bbox for the axes.
532
528
533
529
Parameters
534
530
----------
535
531
ax
536
- renderer
532
+
537
533
538
534
Returns
539
535
-------
@@ -546,15 +542,15 @@ def get_pos_and_bbox(ax, renderer):
546
542
pos = ax .get_position (original = True )
547
543
# pos is in panel co-ords, but we need in figure for the layout
548
544
pos = pos .transformed (fig .transSubfigure - fig .transFigure )
549
- tightbbox = martist ._get_tightbbox_for_layout_only (ax , renderer )
545
+ tightbbox = martist ._get_tightbbox_for_layout_only (ax )
550
546
if tightbbox is None :
551
547
bbox = pos
552
548
else :
553
549
bbox = tightbbox .transformed (fig .transFigure .inverted ())
554
550
return pos , bbox
555
551
556
552
557
- def reposition_axes (layoutgrids , fig , renderer , * ,
553
+ def reposition_axes (layoutgrids , fig , * ,
558
554
w_pad = 0 , h_pad = 0 , hspace = 0 , wspace = 0 ):
559
555
"""
560
556
Reposition all the axes based on the new inner bounding box.
@@ -564,7 +560,7 @@ def reposition_axes(layoutgrids, fig, renderer, *,
564
560
bbox = layoutgrids [sfig ].get_outer_bbox ()
565
561
sfig ._redo_transform_rel_fig (
566
562
bbox = bbox .transformed (trans_fig_to_subfig ))
567
- reposition_axes (layoutgrids , sfig , renderer ,
563
+ reposition_axes (layoutgrids , sfig ,
568
564
w_pad = w_pad , h_pad = h_pad ,
569
565
wspace = wspace , hspace = hspace )
570
566
@@ -592,11 +588,11 @@ def reposition_axes(layoutgrids, fig, renderer, *,
592
588
offset = {'left' : 0 , 'right' : 0 , 'bottom' : 0 , 'top' : 0 }
593
589
for nn , cbax in enumerate (ax ._colorbars [::- 1 ]):
594
590
if ax == cbax ._colorbar_info ['parents' ][0 ]:
595
- reposition_colorbar (layoutgrids , cbax , renderer ,
591
+ reposition_colorbar (layoutgrids , cbax ,
596
592
offset = offset )
597
593
598
594
599
- def reposition_colorbar (layoutgrids , cbax , renderer , * , offset = None ):
595
+ def reposition_colorbar (layoutgrids , cbax , * , offset = None ):
600
596
"""
601
597
Place the colorbar in its new place.
602
598
@@ -605,7 +601,6 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None):
605
601
cbax : Axes
606
602
Axes for the colorbar
607
603
608
- renderer :
609
604
w_pad, h_pad : float
610
605
width and height padding (in fraction of figure)
611
606
hspace, wspace : float
@@ -632,7 +627,7 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None):
632
627
aspect = cbax ._colorbar_info ['aspect' ]
633
628
shrink = cbax ._colorbar_info ['shrink' ]
634
629
635
- cbpos , cbbbox = get_pos_and_bbox (cbax , renderer )
630
+ cbpos , cbbbox = get_pos_and_bbox (cbax )
636
631
637
632
# Colorbar gets put at extreme edge of outer bbox of the subplotspec
638
633
# It needs to be moved in by: 1) a pad 2) its "margin" 3) by
0 commit comments