Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Changes to figure.clf() and suplot_adjust #11086

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

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
13 changes: 13 additions & 0 deletions doc/users/next_whats_new/subplots_adjust_keyword.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
subplots_adjust has a new ``kwarg``: ``rc_default``
---------------------------------------------------

`.Figure.subplots_adjust` and `.pyplot.subplots_adjust` have a new ``kwarg``:
``rc_default`` that determines the default values for the subplot parameters.

The `.figure.SubplotParams` object has a new get method
:meth:`~.SubplotParams.get_subplot_params`





138 changes: 91 additions & 47 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,77 +173,82 @@ def __init__(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
"""
All dimensions are fractions of the figure width or height.
Defaults are given by :rc:`figure.subplot.[name]`.
Defaults are given by :rc:`figure.subplot.*`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would leave [name]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like any of them but I don't know a better one. I think that figure.subplot.[name] make it easier to understand what to write.


Parameters
----------
left : float
left : float, optional
The left side of the subplots of the figure.

right : float
right : float, optional
The right side of the subplots of the figure.

bottom : float
bottom : float, optional
The bottom of the subplots of the figure.

top : float
top : float, optional
The top of the subplots of the figure.

wspace : float
wspace : float, optional
The amount of width reserved for space between subplots,
expressed as a fraction of the average axis width.

hspace : float
hspace : float, optional
The amount of height reserved for space between subplots,
expressed as a fraction of the average axis height.
"""
self.validate = True
self.update(left, bottom, right, top, wspace, hspace)

def __repr__(self):
return ("SubplotParams(left={}, bottom={}, right={}, top={}, "
"wspace={}, hspace={})").format(
self.left, self.bottom, self.right, self.top,
self.wspace, self.hspace)

def update(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
"""
Update the dimensions of the passed parameters. *None* means unchanged.
"""
thisleft = getattr(self, 'left', None)
thisright = getattr(self, 'right', None)
thistop = getattr(self, 'top', None)
thisbottom = getattr(self, 'bottom', None)
thiswspace = getattr(self, 'wspace', None)
thishspace = getattr(self, 'hspace', None)

self._update_this('left', left)
self._update_this('right', right)
self._update_this('bottom', bottom)
self._update_this('top', top)
self._update_this('wspace', wspace)
self._update_this('hspace', hspace)

def reset():
self.left = thisleft
self.right = thisright
self.top = thistop
self.bottom = thisbottom
self.wspace = thiswspace
self.hspace = thishspace
wspace=None, hspace=None, rc_default=False):
"""
Update the dimensions of the passed parameters. *None* means
unchanged if the attribute is set and *rc_default* is *False*, and
:rc:`figure.subplot.*` otherwise.
"""

varDict = dict(left=left, bottom=bottom, right=right, top=top,
wspace=wspace, hspace=hspace)
oldVarDict = {key: getattr(self, key, None) for key in varDict.keys()}

self._update(varDict, rc_default)
if self.validate:
if self.left >= self.right:
reset()
self._update(oldVarDict)
raise ValueError('left cannot be >= right')

if self.bottom >= self.top:
reset()
self._update(oldVarDict)
raise ValueError('bottom cannot be >= top')

def _update_this(self, s, val):
if val is None:
val = getattr(self, s, None)
if val is None:
key = 'figure.subplot.' + s
val = rcParams[key]
def _update(self, varDict, rc_default=None):
for att, value in varDict.items():
if value is None:
if not rc_default:
value = getattr(self, att, None)
if value is None:
key = 'figure.subplot.' + att
value = rcParams[key]

setattr(self, s, val)
setattr(self, att, value)

def get_subplot_params(self):
"""
Returns
-------
subplot_params : dictionary
A dictionary with the subplot parameters
"""
subplot_params = self.__dict__.copy()
del subplot_params['validate']
return subplot_params


class Figure(Artist):
Expand Down Expand Up @@ -1406,8 +1411,11 @@ def clf(self, keep_observers=False):
"""
Clear the figure.

Set *keep_observers* to True if, for example,
a gui widget is tracking the axes in the figure.
Parameters
----------
keep_observers : bool, optional
Set *keep_observers* to True if, for example,
a gui widget is tracking the axes in the figure.
"""
self.suppressComposite = None
self.callbacks = cbook.CallbackRegistry()
Expand All @@ -1426,6 +1434,7 @@ def clf(self, keep_observers=False):
self.texts = []
self.images = []
self.legends = []
self.subplotpars.update(rc_default=True)
if not keep_observers:
self._axobservers = []
self._suptitle = None
Expand Down Expand Up @@ -1915,13 +1924,48 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
return cb

def subplots_adjust(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
wspace=None, hspace=None, rc_default=False):
"""
Update the :class:`SubplotParams` with *kwargs* (defaulting to rc when
*None*) and update the subplot locations.
Tune the subplots layout by updating the subplots parameters and
the subplot locations.

All dimensions are fractions of the figure width or height.

Parameters
----------
left : float, optional
The left side of the subplots of the figure.

right : float, optional
The right side of the subplots of the figure.

bottom : float, optional
The bottom of the subplots of the figure.

top : float, optional
The top of the subplots of the figure.

wspace : float, optional
The amount of width reserved for space between subplots,
expressed as a fraction of the average axis width.

hspace : float, optional
The amount of height reserved for space between subplots,
expressed as a fraction of the average axis height.

rc_default : bool, optional
Determine the defaults. *False*, the default, and the values
are unchanged. *True* and the values are taken from
:rc:`figure.subplot.*`

Notes
-----
The subplots parameters are stored in the `~.Figure` attribute
``subplotpars`` as a `~.SubplotParams` object.
"""
self.subplotpars.update(left, bottom, right, top, wspace, hspace)

self.subplotpars.update(left, bottom, right, top, wspace,
hspace, rc_default)
for ax in self.axes:
if not isinstance(ax, SubplotBase):
# Check if sharing a subplots axis
Expand Down
50 changes: 37 additions & 13 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,25 +1204,49 @@ def twiny(ax=None):


def subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
wspace=None, hspace=None, rc_default=False):
"""
Tune the subplot layout.
Tune the subplots layout by updating the subplots parameters and
the subplot locations.

The parameter meanings (and suggested defaults) are::
All dimensions are fractions of the figure width or height.

left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for space between subplots,
# expressed as a fraction of the average axis width
hspace = 0.2 # the amount of height reserved for space between subplots,
# expressed as a fraction of the average axis height
Parameters
----------
left : float, optional
The left side of the subplots of the figure.

right : float, optional
The right side of the subplots of the figure.

bottom : float, optional
The bottom of the subplots of the figure.

top : float, optional
The top of the subplots of the figure.

wspace : float, optional
The amount of width reserved for space between subplots,
expressed as a fraction of the average axis width.

The actual defaults are controlled by the rc file
hspace : float, optional
The amount of height reserved for space between subplots,
expressed as a fraction of the average axis height.

rc_default : bool, optional
Determine the defaults. *False*, the default, and the values
are unchanged. *True* and the values are taken from
:rc:`figure.subplot.*`


Notes
-----
The subplots parameters are stored in the `~.Figure` attribute
``subplotpars`` as a `~.SubplotParams` object.
"""

fig = gcf()
fig.subplots_adjust(left, bottom, right, top, wspace, hspace)
fig.subplots_adjust(left, bottom, right, top, wspace, hspace, rc_default)


def subplot_tool(targetfig=None):
Expand Down
42 changes: 42 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,45 @@ def test_fspath(fmt, tmpdir):
# All the supported formats include the format name (case-insensitive)
# in the first 100 bytes.
assert fmt.encode("ascii") in file.read(100).lower()


def test_clf_subplotpars():
keys = ('left', 'right', 'bottom', 'top', 'wspace', 'hspace')
rc_params = {key: plt.rcParams['figure.subplot.'+key] for key in keys}

fig = plt.figure(1)
fig.subplots_adjust(left=0.1)
fig.clf()
assert fig.subplotpars.get_subplot_params() == rc_params


def test_suplots_adjust_1():
fig = plt.figure(1)
wspace = 0
fig.subplots_adjust(wspace=wspace)
inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05)
fig.subplots_adjust(**inDict)
inDict['wspace'] = wspace
assert fig.subplotpars.get_subplot_params() == inDict


def test_suplots_adjust_2():
fig = plt.figure(1)
fig.subplots_adjust(wspace=0)
inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05,
rc_default=True)
fig.subplots_adjust(**inDict)
inDict['wspace'] = plt.rcParams['figure.subplot.wspace']
del inDict['rc_default']
assert fig.subplotpars.get_subplot_params() == inDict


def test_suplots_adjust_plt():
plt.figure(1)
plt.subplots_adjust(wspace=0)
inDict = dict(left=0.1, right=0.7, bottom=0, top=0.9, hspace=0.05,
rc_default=True)
plt.subplots_adjust(**inDict)
inDict['wspace'] = plt.rcParams['figure.subplot.wspace']
del inDict['rc_default']
assert plt.gcf().subplotpars.get_subplot_params() == inDict