Skip to content

Commit ea0630a

Browse files
committed
figure: improved auto tight_layout support
1) Use getter and setter instead of property 2) in tight_layout, don't proceed if an Axes is not a SubplotBase 3) in colorbar, make use_gridspec default to True.
1 parent 329085e commit ea0630a

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

lib/matplotlib/figure.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def __init__(self,
254254
linewidth = 0.0, # the default linewidth of the frame
255255
frameon = True, # whether or not to draw the figure frame
256256
subplotpars = None, # default to rc
257-
tight = None, # whether to apply tight_layout
257+
tight_layout = None, # default to rc figure.autolayout
258258
):
259259
"""
260260
*figsize*
@@ -278,7 +278,7 @@ def __init__(self,
278278
*subplotpars*
279279
A :class:`SubplotParams` instance, defaults to rc
280280
281-
*tight*
281+
*tight_layout*
282282
If *False* use *subplotpars*; if *True* adjust subplot
283283
parameters using :meth:`tight_layout`. Defaults to
284284
rc ``figure.autolayout``.
@@ -317,7 +317,7 @@ def __init__(self,
317317
subplotpars = SubplotParams()
318318

319319
self.subplotpars = subplotpars
320-
self._set_tight(tight)
320+
self.set_tight_layout(tight_layout)
321321

322322
self._axstack = AxesStack() # track all figure axes and current axes
323323
self.clf()
@@ -336,15 +336,23 @@ def _set_dpi(self, dpi):
336336
self.callbacks.process('dpi_changed', self)
337337
dpi = property(_get_dpi, _set_dpi)
338338

339-
def _get_tight(self):
339+
def get_tight_layout(self):
340+
"""
341+
Return the Boolean flag, True to use :meth`tight_layout` when drawing.
342+
"""
340343
return self._tight
341-
def _set_tight(self, tight):
344+
345+
def set_tight_layout(self, tight):
346+
"""
347+
Set whether :meth:`tight_layout` is used upon drawing.
348+
If None, the rcParams['figure.autolayout'] value will be set.
349+
350+
ACCEPTS: [True | False | None]
351+
"""
342352
if tight is None:
343353
tight = rcParams['figure.autolayout']
344354
tight = bool(tight)
345355
self._tight = tight
346-
tight = property(_get_tight, _set_tight,
347-
doc="If *True*, use :meth:`tight_layout`")
348356

349357
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
350358
"""
@@ -880,7 +888,7 @@ def draw(self, renderer):
880888
if not self.get_visible(): return
881889
renderer.open_group('figure')
882890

883-
if self.tight and self.axes:
891+
if self.get_tight_layout() and self.axes:
884892
try:
885893
self.tight_layout(renderer)
886894
except ValueError:
@@ -1262,7 +1270,7 @@ def colorbar(self, mappable, cax=None, ax=None, **kw):
12621270
"""
12631271
if ax is None:
12641272
ax = self.gca()
1265-
use_gridspec = kw.pop("use_gridspec", False)
1273+
use_gridspec = kw.pop("use_gridspec", True)
12661274
if cax is None:
12671275
if use_gridspec and isinstance(ax, SubplotBase):
12681276
cax, kw = cbar.make_axes_gridspec(ax, **kw)
@@ -1405,6 +1413,12 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
14051413

14061414
from tight_layout import get_renderer, get_tight_layout_figure
14071415

1416+
no_go = [ax for ax in self.axes if not isinstance(ax, SubplotBase)]
1417+
if no_go:
1418+
warnings.Warn("Cannot use tight_layout;"
1419+
" all Axes must descend from SubplotBase")
1420+
return
1421+
14081422
if renderer is None:
14091423
renderer = get_renderer(self)
14101424

0 commit comments

Comments
 (0)