Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 90 additions & 11 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
):
"""
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/figure.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,3 +1659,12 @@ def test_not_visible_figure():
fig.savefig(buf, format='svg')
buf.seek(0)
assert '<g ' not in buf.read()

def test_fig_get_set():
varnames = filter(lambda var: var not in ['self', 'kwargs', 'args'],
Figure.__init__.__code__.co_varnames)
fig = plt.figure()
for var in varnames:
# if getattr fails then the getter and setter does not exist
getfunc = getattr(fig, f"get_{var}")
setfunc = getattr(fig, f"set_{var}")