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 5 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
137 changes: 90 additions & 47 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,43 +1211,7 @@ 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)

if cax is None:
if ax is None:
raise ValueError(
'Unable to determine Axes to steal space for Colorbar. '
'Either provide the *cax* argument to use as the Axes for '
'the Colorbar, provide the *ax* argument to steal space '
'from it, or add *mappable* to an Axes.')
fig = ( # Figure of first axes; logic copied from make_axes.
[*ax.flat] if isinstance(ax, np.ndarray)
else [*ax] if np.iterable(ax)
else [ax])[0].figure
current_ax = fig.gca()
if (fig.get_layout_engine() is not None and
not fig.get_layout_engine().colorbar_gridspec):
use_gridspec = False
if (use_gridspec
and isinstance(ax, mpl.axes._base._AxesBase)
and ax.get_subplotspec()):
cax, kwargs = cbar.make_axes_gridspec(ax, **kwargs)
else:
cax, kwargs = cbar.make_axes(ax, **kwargs)
# make_axes calls add_{axes,subplot} which changes gca; undo that.
fig.sca(current_ax)
cax.grid(visible=False, which='both', axis='both')

NON_COLORBAR_KEYS = [ # remove kws that cannot be passed to Colorbar
'fraction', 'pad', 'shrink', 'aspect', 'anchor', 'panchor']
cb = cbar.Colorbar(cax, mappable, **{
k: v for k, v in kwargs.items() if k not in NON_COLORBAR_KEYS})
cax.figure.stale = True
return cb
"""

def subplots_adjust(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
Expand Down Expand Up @@ -2345,17 +2309,17 @@ def __repr__(self):
)

def __init__(self,
figsize=None,
dpi=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 +2824,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
67 changes: 67 additions & 0 deletions lib/matplotlib/test_figure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.figure import Figure


def test_set_subplotpars():
subplotparams_keys = ["left", "bottom", "right", "top", "wspace", "hspace"]
fig = plt.figure()
subplotparams = fig.get_subplotpars()
test_dict = {}
default_dict = {}
for key in subplotparams_keys:
attr = getattr(subplotparams, key)
assert attr == mpl.rcParams[f"figure.subplot.{key}"]
default_dict[key] = attr
test_dict[key] = attr * 2

subplotparams.update(left=test_dict['left'])
assert fig.get_subplotpars().left == test_dict['left']

fig.subplots_adjust(**default_dict)
assert fig.get_subplotpars().left == default_dict['left']

fig.set_subplotpars(test_dict)
for key, value in test_dict.items():
assert getattr(fig.get_subplotpars(), key) == value

test_subplotparams = SubplotParams()
fig.set_subplotpars(test_subplotparams)
for key, value in default_dict.items():
assert getattr(fig.get_subplotpars(), key) == value

fig.set_subplotpars(test_dict)
for key, value in test_dict.items():
assert getattr(fig.get_subplotpars(), key) == value

test_dict['foo'] = 'bar'
with pytest.warns(UserWarning,
match="'foo' is not a valid key for set_subplotpars;"
" this key was ignored"):
fig.set_subplotpars(test_dict)

with pytest.raises(TypeError,
match="subplotpars must be a dictionary of "
"keyword-argument pairs or "
"an instance of SubplotParams()"):
fig.set_subplotpars(['foo'])

fig.set_subplotpars({})
with pytest.raises(AttributeError): # test_dict['foo'] = 'bar'
# but fig.get_subplotpars().foo should be invalid
for key, value in test_dict.items():
assert getattr(fig.get_subplotpars(), key) == value


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}")

assert test_fig_get_set() is None
0