def make_layoutgrids_gs(layoutgrids, gs): """ Make the layoutgrid for a gridspec (and anything nested in the gridspec) """ if gs in layoutgrids or gs.figure is None: return layoutgrids # in order to do constrained_layout there has to be at least *one* # gridspec in the tree: layoutgrids['hasgrids'] = True if not hasattr(gs, '_subplot_spec'): # normal gridspec parent = layoutgrids[gs.figure] layoutgrids[gs] = mlayoutgrid.LayoutGrid( parent=parent, parent_inner=True, name='gridspec', ncols=gs._ncols, nrows=gs._nrows, width_ratios=gs.get_width_ratios(), height_ratios=gs.get_height_ratios()) else: # this is a gridspecfromsubplotspec: subplot_spec = gs._subplot_spec parentgs = subplot_spec.get_gridspec() # if a nested gridspec it is possible the parent is not in there yet: if parentgs not in layoutgrids: layoutgrids = make_layoutgrids_gs(layoutgrids, parentgs) subspeclb = layoutgrids[parentgs] # gridspecfromsubplotspec need an outer container: # get a unique representation: rep = (gs, 'top') if rep not in layoutgrids: layoutgrids[rep] = mlayoutgrid.LayoutGrid( parent=subspeclb, name='top', nrows=1, ncols=1, parent_pos=(subplot_spec.rowspan, subplot_spec.colspan)) layoutgrids[gs] = mlayoutgrid.LayoutGrid( parent=layoutgrids[rep], name='gridspec', nrows=gs._nrows, ncols=gs._ncols, width_ratios=gs.get_width_ratios(), height_ratios=gs.get_height_ratios()) return layoutgrids
def __init__(self, nrows, ncols, subplot_spec, wspace=None, hspace=None, height_ratios=None, width_ratios=None): """ The number of rows and number of columns of the grid need to be set. An instance of SubplotSpec is also needed to be set from which the layout parameters will be inherited. The wspace and hspace of the layout can be optionally specified or the default values (from the figure or rcParams) will be used. """ self._wspace = wspace self._hspace = hspace self._subplot_spec = subplot_spec self.figure = self._subplot_spec.get_gridspec().figure super().__init__(nrows, ncols, width_ratios=width_ratios, height_ratios=height_ratios) # do the layoutgrids for constrained_layout: subspeclb = subplot_spec.get_gridspec()._layoutgrid if subspeclb is None: self._layoutgrid = None else: # this _toplayoutbox is a container that spans the cols and # rows in the parent gridspec. Not yet implemented, # but we do this so that it is possible to have subgridspec # level artists. self._toplayoutgrid = layoutgrid.LayoutGrid( parent=subspeclb, name=subspeclb.name + '.top' + layoutgrid.seq_id(), nrows=1, ncols=1, parent_pos=(subplot_spec.rowspan, subplot_spec.colspan)) self._layoutgrid = layoutgrid.LayoutGrid( parent=self._toplayoutgrid, name=(self._toplayoutgrid.name + '.gridspec' + layoutgrid.seq_id()), nrows=nrows, ncols=ncols, width_ratios=width_ratios, height_ratios=height_ratios)
def make_layoutgrids(fig, layoutgrids, rect=(0, 0, 1, 1)): """ Make the layoutgrid tree. (Sub)Figures get a layoutgrid so we can have figure margins. Gridspecs that are attached to axes get a layoutgrid so axes can have margins. """ if layoutgrids is None: layoutgrids = dict() layoutgrids['hasgrids'] = False if not hasattr(fig, '_parent'): # top figure; pass rect as parent to allow user-specified # margins layoutgrids[fig] = mlayoutgrid.LayoutGrid(parent=rect, name='figlb') else: # subfigure gs = fig._subplotspec.get_gridspec() # it is possible the gridspec containing this subfigure hasn't # been added to the tree yet: layoutgrids = make_layoutgrids_gs(layoutgrids, gs) # add the layoutgrid for the subfigure: parentlb = layoutgrids[gs] layoutgrids[fig] = mlayoutgrid.LayoutGrid( parent=parentlb, name='panellb', parent_inner=True, nrows=1, ncols=1, parent_pos=(fig._subplotspec.rowspan, fig._subplotspec.colspan)) # recursively do all subfigures in this figure... for sfig in fig.subfigs: layoutgrids = make_layoutgrids(sfig, layoutgrids) # for each axes at the local level add its gridspec: for ax in fig._localaxes: if hasattr(ax, 'get_subplotspec'): gs = ax.get_subplotspec().get_gridspec() layoutgrids = make_layoutgrids_gs(layoutgrids, gs) return layoutgrids
def __init__(self, nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None): """ Parameters ---------- nrows, ncols : int The number of rows and columns of the grid. figure : `~.figure.Figure`, optional Only used for constrained layout to create a proper layoutgrid. left, right, top, bottom : float, optional Extent of the subplots as a fraction of figure width or height. Left cannot be larger than right, and bottom cannot be larger than top. If not given, the values will be inferred from a figure or rcParams at draw time. See also `GridSpec.get_subplot_params`. wspace : float, optional The amount of width reserved for space between subplots, expressed as a fraction of the average axis width. If not given, the values will be inferred from a figure or rcParams when necessary. See also `GridSpec.get_subplot_params`. hspace : float, optional The amount of height reserved for space between subplots, expressed as a fraction of the average axis height. If not given, the values will be inferred from a figure or rcParams when necessary. See also `GridSpec.get_subplot_params`. width_ratios : array-like of length *ncols*, optional Defines the relative widths of the columns. Each column gets a relative width of ``width_ratios[i] / sum(width_ratios)``. If not given, all columns will have the same width. height_ratios : array-like of length *nrows*, optional Defines the relative heights of the rows. Each column gets a relative height of ``height_ratios[i] / sum(height_ratios)``. If not given, all rows will have the same height. """ self.left = left self.bottom = bottom self.right = right self.top = top self.wspace = wspace self.hspace = hspace self.figure = figure super().__init__(nrows, ncols, width_ratios=width_ratios, height_ratios=height_ratios) # set up layoutgrid for constrained_layout: self._layoutgrid = None if self.figure is None or not self.figure.get_constrained_layout(): self._layoutgrid = None else: self._toplayoutbox = self.figure._layoutgrid self._layoutgrid = layoutgrid.LayoutGrid( parent=self.figure._layoutgrid, parent_inner=True, name=(self.figure._layoutgrid.name + '.gridspec' + layoutgrid.seq_id()), ncols=ncols, nrows=nrows, width_ratios=width_ratios, height_ratios=height_ratios)