diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 86a9fdd1387e..0c7f997e0728 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1211,8 +1211,8 @@ def colorbar( However, this has negative consequences in other circumstances, e.g. with semi-transparent images (alpha < 1) and colorbar extensions; therefore, this workaround is not used by default (see issue #1188). - - """ + """ + if ax is None: ax = getattr(mappable, "axes", None) @@ -2345,17 +2345,17 @@ def __repr__(self): ) def __init__(self, - figsize=None, + figsize=None, dpi=None, *, - facecolor=None, - edgecolor=None, - linewidth=0.0, - frameon=None, - subplotpars=None, # rc figure.subplot.* - tight_layout=None, # rc figure.autolayout - constrained_layout=None, # rc figure.constrained_layout.use - layout=None, + facecolor=None, + edgecolor=None, + linewidth=0.0, + frameon=None, + subplotpars=None, # rc figure.subplot.* + tight_layout=None, # rc figure.autolayout + constrained_layout=None, # rc figure.constrained_layout.use + layout=None, **kwargs ): """ @@ -2860,6 +2860,85 @@ def set_canvas(self, canvas): """ self.canvas = canvas + def set_subplotpars(self, subplotparams={}): + """ + Set the subplot layout parameters. + Accepts either a `.SubplotParams` object, from which the relevant + parameters are copied, or a dictionary of subplot layout parameters. + If a dictionary is provided, this function is a convenience wrapper for + `matplotlib.figure.Figure.subplots_adjust` + Parameters + ---------- + subplotparams : `~matplotlib.figure.SubplotParams` or dict with keys \ +"left", "bottom", "right", 'top", "wspace", "hspace"] , optional + SubplotParams object to copy new subplot parameters from, or a dict + of SubplotParams constructor arguments. + By default, an empty dictionary is passed, which maintains the + current state of the figure's `.SubplotParams` + See Also + -------- + matplotlib.figure.Figure.subplots_adjust + matplotlib.figure.Figure.get_subplotpars + """ + subplotparams_args = ["left", "bottom", "right", + "top", "wspace", "hspace"] + kwargs = {} + if isinstance(subplotparams, SubplotParams): + for key in subplotparams_args: + kwargs[key] = getattr(subplotparams, key) + elif isinstance(subplotparams, dict): + for key in subplotparams.keys(): + if key in subplotparams_args: + kwargs[key] = subplotparams[key] + else: + _api.warn_external( + f"'{key}' is not a valid key for set_subplotpars;" + " this key was ignored.") + else: + raise TypeError( + "subplotpars must be a dictionary of keyword-argument pairs or" + " an instance of SubplotParams()") + if kwargs == {}: + self.set_subplotpars(self.get_subplotpars()) + self.subplots_adjust(**kwargs) + + def get_subplotpars(self): + """ + Return the `.SubplotParams` object associated with the Figure. + Returns + ------- + `.SubplotParams` + See Also + -------- + matplotlib.figure.Figure.subplots_adjust + matplotlib.figure.Figure.get_subplotpars + """ + return self.subplotpars + + def set_figsize(self, fig_size_params): + self.set_size_inches(fig_size_params[0], fig_size_params[1]) + """ + Calls all the set_size_inches() methods of the figure and its subfigures. + passes Parameters + """ + def get_figsize(self): + """ + Returns the size of the figure in inches + """ + return self.get_size_inches() + + def set_layout(self, layout_params): + """ + Sets the layout of the figure. + """ + self.set_layout_engine(layout_params) + + def get_layout(self): + """ + Returns the layout of the figure. + """ + return self.get_layout_engine() + @_docstring.interpd def figimage(self, X, xo=0, yo=0, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, origin=None, resize=False, **kwargs): diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 687ae9e500d0..03e939694dc8 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -361,6 +361,12 @@ class Figure(FigureBase): def draw_without_rendering(self) -> None: ... def draw_artist(self, a: Artist) -> None: ... def add_axobserver(self, func: Callable[[Figure], Any]) -> None: ... + def get_subplotpars(self) -> SubplotParams: ... + def set_subplotpars(self, val: SubplotParams) -> None: ... + def get_figsize(self) -> tuple[float, float]: ... + def set_figsize(self, val: tuple[float, float]) -> None: ... + def set_layout(self, val: Literal["constrained", "compressed", "tight"]) -> None: ... + def get_layout(self) -> Literal["constrained", "compressed", "tight"]: ... def savefig( self, fname: str | os.PathLike | IO, diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 6d6a3d772f4e..6403998cb454 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1659,3 +1659,12 @@ def test_not_visible_figure(): fig.savefig(buf, format='svg') buf.seek(0) assert '