8000 Fixes : Add setter/getter methods for all keyword parameters to Figure.__init__ #24617 by Lambxx · Pull Request #27257 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fixes : Add setter/getter methods for all keyword parameters to Figure.__init__ #24617 #27257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,8 @@
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 __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 @@
"""
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"]

Check warning on line 2884 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2884

Added line #L2884 was not covered by tests
kwargs = {}
if isinstance(subplotparams, SubplotParams):

Check warning on line 2886 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2886

Added line #L2886 was not covered by tests
for key in subplotparams_args:
kwargs[key] = getattr(subplotparams, key)
elif isinstance(subplotparams, dict):

Check warning on line 2889 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2889

Added line #L2889 was not covered by tests
for key in subplotparams.keys():
if key in subplotparams_args:
kwargs[key] = subplotparams[key]
else:

Check warning on line 2893 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2893

Added line #L2893 was not covered by tests
_api.warn_external(
f"'{key}' is not a valid key for set_subplotpars;"

Check warning on line 2895 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2895

Added line #L2895 was not covered by tests
" this key was ignored.")
else:
raise TypeError(
"subplotpars must be a dictionary of keyword-argument pairs or"

Check warning on line 2899 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2899

Added line #L2899 was not covered by tests
" an instance of SubplotParams()")
if kwargs == {}:
self.set_subplotpars(self.get_subplotpars())
self.subplots_adjust(**kwargs)

Check warning on line 2904 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2903-L2904

Added lines #L2903 - L2904 were not covered by tests
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

Check warning on line 2917 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2917

Added line #L2917 was not covered by tests
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.

Check warning on line 2921 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2920-L2921

Added lines #L2920 - L2921 were not covered by tests
passes Parameters
"""
def get_figsize(self):
"""
Returns the size of the figure in inches
"""
return self.get_size_inches()

10000

Check warning on line 2929 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2929

Added line #L2929 was not covered by tests
def set_layout(self, layout_params):
"""
Sets the layout of the figure.
"""
self.set_layout_engine(layout_params)

Check warning on line 2935 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2935

Added line #L2935 was not covered by tests
def get_layout(self):
"""
Returns the layout of the figure.
"""
return self.get_layout_engine()

Check warning on line 2941 in lib/matplotlib/figure.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/figure.py#L2941

Added line #L2941 was not covered by tests
@_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}")
0