Skip to content

Commit b8a0139

Browse files
committed
Merge pull request matplotlib#1202 from leejjoon/fix-1055
refactored grid_spec.tight_layout and fixed matplotlib#1055
2 parents 8b8d24f + 1eb17b8 commit b8a0139

File tree

2 files changed

+27
-60
lines changed

2 files changed

+27
-60
lines changed

lib/matplotlib/gridspec.py

+17-58
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import matplotlib.transforms as mtransforms
2323

2424
import numpy as np
25+
import warnings
2526

2627
class GridSpecBase(object):
2728
"""
@@ -241,7 +242,8 @@ def update(self, **kwargs):
241242
ax._sharey.update_params()
242243
ax.set_position(ax._sharey.figbox)
243244
else:
244-
if ax.get_subplotspec().get_gridspec() == self:
245+
ss = ax.get_subplotspec().get_topmost_subplotspec()
246+
if ss.get_gridspec() == self:
245247
ax.update_params()
246248
ax.set_position(ax.figbox)
247249

@@ -287,67 +289,24 @@ def tight_layout(self, fig, renderer=None, pad=1.08, h_pad=None, w_pad=None, rec
287289
labels) will fit into. Default is (0, 0, 1, 1).
288290
"""
289291

290-
from tight_layout import auto_adjust_subplotpars, get_renderer
292+
from tight_layout import (get_subplotspec_list,
293+
get_tight_layout_figure,
294+
get_renderer)
295+
296+
subplotspec_list = get_subplotspec_list(fig.axes, grid_spec=self)
297+
if None in subplotspec_list:
298+
warnings.warn("This figure includes Axes that are not "
299+
"compatible with tight_layout, so its "
300+
"results might be incorrect.")
291301

292302
if renderer is None:
293303
renderer = get_renderer(fig)
294304

295-
subplot_list = []
296-
num1num2_list = []
297-
subplot_dict = {}
298-
299-
for ax in fig.axes:
300-
locator = ax.get_axes_locator()
301-
if hasattr(locator, "get_subplotspec"):
302-
subplotspec = locator.get_subplotspec()
303-
elif hasattr(ax, "get_subplotspec"):
304-
subplotspec = ax.get_subplotspec()
305-
else:
306-
continue
307-
308-
if subplotspec.get_gridspec() != self: continue
309-
310-
subplots = subplot_dict.get(subplotspec, [])
311-
312-
if not subplots:
313-
_, _, num1, num2 = subplotspec.get_geometry()
314-
num1num2_list.append((num1, num2))
315-
subplot_list.append(subplots)
316-
317-
subplots.append(ax)
318-
319-
320-
kwargs = auto_adjust_subplotpars(fig, renderer,
321-
nrows_ncols=self.get_geometry(),
322-
num1num2_list=num1num2_list,
323-
subplot_list=subplot_list,
324-
pad=pad, h_pad=h_pad, w_pad=w_pad)
325-
326-
if rect is not None:
327-
# if rect is given, the whole subplots area (including
328-
# labels) will fit into the rect instead of the
329-
# figure. Note that the rect argument of
330-
# *auto_adjust_subplotpars* specify the area that will be
331-
# covered by the total area of axes.bbox. Thus we call
332-
# auto_adjust_subplotpars twice, where the second run
333-
# with adjusted rect parameters.
334-
335-
left, bottom, right, top = rect
336-
if left is not None: left += kwargs["left"]
337-
if bottom is not None: bottom += kwargs["bottom"]
338-
if right is not None: right -= (1 - kwargs["right"])
339-
if top is not None: top -= (1 - kwargs["top"])
340-
341-
#if h_pad is None: h_pad = pad
342-
#if w_pad is None: w_pad = pad
343-
344-
kwargs = auto_adjust_subplotpars(fig, renderer,
345-
nrows_ncols=self.get_geometry(),
346-
num1num2_list=num1num2_list,
347-
subplot_list=subplot_list,
348-
pad=pad, h_pad=h_pad, w_pad=w_pad,
349-
rect=(left, bottom, right, top))
350-
305+
kwargs = get_tight_layout_figure(fig, fig.axes, subplotspec_list,
306+
renderer,
307+
pad=pad, h_pad=h_pad, w_pad=w_pad,
308+
rect=rect,
309+
)
351310

352311
self.update(**kwargs)
353312

lib/matplotlib/tight_layout.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ def get_renderer(fig):
209209
return renderer
210210

211211

212-
def get_subplotspec_list(axes_list):
212+
def get_subplotspec_list(axes_list, grid_spec=None):
213213
"""
214214
Return a list of subplotspec from the given list of axes. For an
215215
instance of axes that does not support subplotspec, None is
216216
inserted in the list.
217217
218+
If grid_spec is given, None is inserted for those not from
219+
the given grid_spec.
220+
218221
"""
219222
subplotspec_list = []
220223
for ax in axes_list:
@@ -225,11 +228,16 @@ def get_subplotspec_list(axes_list):
225228
if hasattr(axes_or_locator, "get_subplotspec"):
226229
subplotspec = axes_or_locator.get_subplotspec()
227230
subplotspec = subplotspec.get_topmost_subplotspec()
228-
if subplotspec.get_gridspec().locally_modified_subplot_params():
231+
gs = subplotspec.get_gridspec()
232+
if grid_spec is not None:
233+
if gs != grid_spec:
234+
subplotspec = None
235+
elif gs.locally_modified_subplot_params():
229236
subplotspec = None
230237
else:
231238
subplotspec = None
232239

240+
233241
subplotspec_list.append(subplotspec)
234242

235243
return subplotspec_list

0 commit comments

Comments
 (0)