Skip to content

Commit 1d80e7f

Browse files
committed
tight_layout.py: pep8 fix and some refactoring
1 parent 77e245a commit 1d80e7f

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

lib/matplotlib/figure.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,17 +1422,20 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
14221422
labels) will fit into. Default is (0, 0, 1, 1).
14231423
"""
14241424

1425-
from tight_layout import get_renderer, get_tight_layout_figure, \
1426-
get_subplotspec_list
1425+
from tight_layout import (get_renderer, get_tight_layout_figure,
1426+
get_subplotspec_list)
14271427

1428-
if None in get_subplotspec_list(self.axes):
1429-
warnings.warn("tight_layout can only process Axes that descend "
1430-
"from SubplotBase; results might be incorrect.")
1428+
subplotspec_list = get_subplotspec_list(self.axes)
1429+
if None in subplotspec_list:
1430+
warnings.warn("This figure includes Axes that are not "
1431+
"compatible with tight_layout, so its "
1432+
"results might be incorrect.")
14311433

14321434
if renderer is None:
14331435
renderer = get_renderer(self)
14341436

1435-
kwargs = get_tight_layout_figure(self, self.axes, renderer,
1437+
kwargs = get_tight_layout_figure(self, self.axes, subplotspec_list,
1438+
renderer,
14361439
pad=pad, h_pad=h_pad, w_pad=w_pad,
14371440
rect=rect)
14381441

lib/matplotlib/tight_layout.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,24 @@ def get_subplotspec_list(axes_list):
218218
"""
219219
subplotspec_list = []
220220
for ax in axes_list:
221-
locator = ax.get_axes_locator()
222-
if hasattr(locator, "get_subplotspec"):
223-
subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
224-
elif hasattr(ax, "get_subplotspec"):
225-
subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
221+
axes_or_locator = ax.get_axes_locator()
222+
if axes_or_locator is None:
223+
axes_or_locator = ax
224+
225+
if hasattr(axes_or_locator, "get_subplotspec"):
226+
subplotspec = axes_or_locator.get_subplotspec()
227+
subplotspec = subplotspec.get_topmost_subplotspec()
228+
if subplotspec.get_gridspec().locally_modified_subplot_params():
229+
subplotspec = None
226230
else:
227231
subplotspec = None
228232

229-
if subplotspec is not None and \
230-
subplotspec.get_gridspec().locally_modified_subplot_params():
231-
232-
subplotspec = None
233-
234233
subplotspec_list.append(subplotspec)
235234

236235
return subplotspec_list
237236

238237

239-
def get_tight_layout_figure(fig, axes_list, renderer,
238+
def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
240239
pad=1.08, h_pad=None, w_pad=None, rect=None):
241240
"""
242241
Return subplot parameters for tight-layouted-figure with specified
@@ -248,6 +247,9 @@ def get_tight_layout_figure(fig, axes_list, renderer,
248247
249248
*axes_list* : a list of axes
250249
250+
*subplotspec_list* : a list of subplotspec associated with each
251+
axes in axes_list
252+
251253
*renderer* : renderer instance
252254
253255
*pad* : float
@@ -265,7 +267,6 @@ def get_tight_layout_figure(fig, axes_list, renderer,
265267
"""
266268

267269

268-
subplotspec_list = []
269270
subplot_list = []
270271
nrows_list = []
271272
ncols_list = []
@@ -275,8 +276,10 @@ def get_tight_layout_figure(fig, axes_list, renderer,
275276
# same subplot_interface (e.g, axes_grid1). Thus
276277
# we need to join them together.
277278

279+
subplotspec_list2 = []
280+
278281
for ax, subplotspec in zip(axes_list,
279-
get_subplotspec_list(axes_list)):
282+
subplotspec_list):
280283
if subplotspec is None:
281284
continue
282285

@@ -286,7 +289,7 @@ def get_tight_layout_figure(fig, axes_list, renderer,
286289
myrows, mycols, _, _ = subplotspec.get_geometry()
287290
nrows_list.append(myrows)
288291
ncols_list.append(mycols)
289-
subplotspec_list.append(subplotspec)
292+
subplotspec_list2.append(subplotspec)
290293
subplot_list.append(subplots)
291294
ax_bbox_list.append(subplotspec.get_position(fig))
292295

@@ -296,7 +299,7 @@ def get_tight_layout_figure(fig, axes_list, renderer,
296299
max_ncols = max(ncols_list)
297300

298301
num1num2_list = []
299-
for subplotspec in subplotspec_list:
302+
for subplotspec in subplotspec_list2:
300303
rows, cols, num1, num2 = subplotspec.get_geometry()
301304
div_row, mod_row = divmod(max_nrows, rows)
302305
div_col, mod_col = divmod(max_ncols, cols)

0 commit comments

Comments
 (0)