code: def do_constrained_layout(
      fig,
      h_pad,
      w_pad,
      hspace=None,
      wspace=None,
      rect=(0, 0, 1, 1),
      compress=False,
  ):
      """
      Do the constrained_layout.  Called at draw time in
       ``figure.constrained_layout()``
  
      Parameters
      ----------
      fig : `~matplotlib.figure.Figure`
          `.Figure` instance to do the layout in.
  
      h_pad, w_pad : float
        Padding around the Axes elements in figure-normalized units.
  
      hspace, wspace : float
         Fraction of the figure to dedicate to space between the
         Axes.  These are evenly spread between the gaps between the Axes.
         A value of 0.2 for a three-column layout would have a space
         of 0.1 of the figure width between each column.
         If h/wspace < h/w_pad, then the pads are used instead.
  
      rect : tuple of 4 floats
          Rectangle in figure coordinates to perform constrained layout in
          [left, bottom, width, height], each from 0-1.
  
      compress : bool
          Whether to shift Axes so that white space in between them is
          removed. This is useful for simple grids of fixed-aspect Axes (e.g.
          a grid of images).
  
      Returns
      -------
      layoutgrid : private debugging structure
      """
  
      renderer = fig._get_renderer()
      # make layoutgrid tree...
      layoutgrids = make_layoutgrids(fig, None, rect=rect)
      if not layoutgrids["hasgrids"]:
          _api.warn_external(
              "There are no gridspecs with layoutgrids. "
              "Possibly did not call parent GridSpec with the"
              ' "figure" keyword'
          )
          return
  
      for _ in range(2):
          # do the algorithm twice.  This has to be done because decorations
          # change size after the first re-position (i.e. x/yticklabels get
          # larger/smaller).  This second reposition tends to be much milder,
          # so doing twice makes things work OK.
  
          # make margins for all the Axes and subfigures in the
          # figure.  Add margins for colorbars...
          make_layout_margins(
              layoutgrids,
              fig,
              renderer,
              h_pad=h_pad,
              w_pad=w_pad,
              hspace=hspace,
              wspace=wspace,
          )
          make_margin_suptitles(
              layoutgrids, fig, renderer, h_pad=h_pad, w_pad=w_pad
          )
  
          # if a layout is such that a columns (or rows) margin has no
          # constraints, we need to make all such instances in the grid
          # match in margin size.
          match_submerged_margins(layoutgrids, fig)
  
          # update all the variables in the layout.
          layoutgrids[fig].update_variables()
  
          warn_collapsed = (
              "constrained_layout not applied because "
              "axes sizes collapsed to zero.  Try making "
              "figure larger or Axes decorations smaller."
          )
          if check_no_collapsed_axes(layoutgrids, fig):
              reposition_axes(
                  layoutgrids,
                  fig,
                  renderer,
                  h_pad=h_pad,
                  w_pad=w_pad,
                  hspace=hspace,
                  wspace=wspace,
              )
              if compress:
                  layoutgrids = compress_fixed_aspect(layoutgrids, fig)
                  layoutgrids[fig].update_variables()
                  if check_no_collapsed_axes(layoutgrids, fig):
                      reposition_axes(
                          layoutgrids,
                          fig,
                          renderer,
                          h_pad=h_pad,
                          w_pad=w_pad,
                          hspace=hspace,
                          wspace=wspace,
                      )
                  else:
                      _api.warn_external(warn_collapsed)
          else:
              _api.warn_external(warn_collapsed)
          reset_margins(layoutgrids, fig)
      return layoutgrids
  
file_path: matplotlib/lib/matplotlib/_constrained_layout.py
imports: import logging
  import numpy
  from matplotlib import _api, artist
  import matplotlib.transforms
  import matplotlib._layoutgrid
label_desc: Use image node_id 1 for calling a global function with example usage: do_constrained_layout(fig, h_pad, w_pad, hspace, wspace, rect, compress) and returns: layoutgrids