Skip to content

Commit 15f2a77

Browse files
committed
Merge pull request matplotlib#1360 from NelleV/pep8_tight_layout
PEP8 fixes on tight_layout.py
2 parents 73a4153 + c887b65 commit 15f2a77

File tree

1 file changed

+86
-64
lines changed

1 file changed

+86
-64
lines changed

lib/matplotlib/tight_layout.py

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""
2-
This module provides routines to adjust subplot params so that
3-
subplots are nicely fit in the figure. In doing so, only axis labels,
4-
tick labels and axes titles are currently considered.
2+
This module provides routines to adjust subplot params so that subplots are
3+
nicely fit in the figure. In doing so, only axis labels, tick labels and axes
4+
titles are currently considered.
55
66
Internally, it assumes that the margins (left_margin, etc.) which are
7-
differences between ax.get_tightbbox and ax.bbox are independent of
8-
axes position. This may fail if Axes.adjustable is datalim. Also, This
9-
will fail for some cases (for example, left or right margin is affected by xlabel).
10-
7+
differences between ax.get_tightbbox and ax.bbox are independent of axes
8+
position. This may fail if Axes.adjustable is datalim. Also, This will fail
9+
for some cases (for example, left or right margin is affected by xlabel).
1110
"""
1211

1312
import warnings
@@ -19,16 +18,18 @@
1918
rcParams = matplotlib.rcParams
2019

2120

22-
2321
def _get_left(tight_bbox, axes_bbox):
2422
return axes_bbox.xmin - tight_bbox.xmin
2523

24+
2625
def _get_right(tight_bbox, axes_bbox):
2726
return tight_bbox.xmax - axes_bbox.xmax
2827

28+
2929
def _get_bottom(tight_bbox, axes_bbox):
3030
return axes_bbox.ymin - tight_bbox.ymin
3131

32+
3233
def _get_top(tight_bbox, axes_bbox):
3334
return tight_bbox.ymax - axes_bbox.ymax
3435

@@ -59,55 +60,64 @@ def auto_adjust_subplotpars(fig, renderer,
5960
list of subplots that will be used to calcuate optimal subplot_params.
6061
6162
pad : float
62-
padding between the figure edge and the edges of subplots, as a fraction of the font-size.
63+
padding between the figure edge and the edges of subplots, as a fraction
64+
of the font-size.
6365
h_pad, w_pad : float
6466
padding (height/width) between edges of adjacent subplots.
6567
Defaults to `pad_inches`.
6668
6769
rect
6870
[left, bottom, right, top] in normalized (0, 1) figure coordinates.
6971
"""
70-
71-
7272
rows, cols = nrows_ncols
7373

74-
pad_inches = pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
74+
pad_inches = pad * FontProperties(
75+
size=rcParams["font.size"]).get_size_in_points() / 72.
7576

7677
if h_pad is not None:
77-
vpad_inches = h_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
78+
vpad_inches = h_pad * FontProperties(
79+
size=rcParams["font.size"]).get_size_in_points() / 72.
7880
else:
7981
vpad_inches = pad_inches
8082

8183
if w_pad is not None:
82-
hpad_inches = w_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
84+
hpad_inches = w_pad * FontProperties(
85+
size=rcParams["font.size"]).get_size_in_points() / 72.
8386
else:
8487
hpad_inches = pad_inches
8588

86-
8789
if len(subplot_list) == 0:
8890
raise RuntimeError("")
8991

9092
if len(num1num2_list) != len(subplot_list):
9193
raise RuntimeError("")
9294

9395
if rect is None:
94-
margin_left, margin_bottom, margin_right, margin_top = None, None, None, None
96+
margin_left = None
97+
margin_bottom = None
98+
margin_right = None
99+
margin_top = None
95100
else:
96101
margin_left, margin_bottom, _right, _top = rect
97-
if _right: margin_right = 1. - _right
98-
else: margin_right = None
99-
if _top: margin_top = 1. - _top
100-
else: margin_top = None
102+
if _right:
103+
margin_right = 1. - _right
104+
else:
105+
margin_right = None
106+
if _top:
107+
margin_top = 1. - _top
108+
else:
109+
margin_top = None
101110

102-
vspaces = [[] for i in range((rows+1)*cols)]
103-
hspaces = [[] for i in range(rows*(cols+1))]
111+
vspaces = [[] for i in range((rows + 1) * cols)]
112+
hspaces = [[] for i in range(rows * (cols + 1))]
104113

105114
union = Bbox.union
106115

107116
if ax_bbox_list is None:
108117
ax_bbox_list = []
109118
for subplots in subplot_list:
110-
ax_bbox = union([ax.get_position(original=True) for ax in subplots])
119+
ax_bbox = union([ax.get_position(original=True)
120+
for ax in subplots])
111121
ax_bbox_list.append(ax_bbox)
112122

113123
for subplots, ax_bbox, (num1, num2) in zip(subplot_list,
@@ -117,76 +127,85 @@ def auto_adjust_subplotpars(fig, renderer,
117127
#ax_bbox = union([ax.get_position(original=True) for ax in subplots])
118128

119129
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])
120-
tight_bbox = TransformedBbox(tight_bbox_raw, fig.transFigure.inverted())
130+
tight_bbox = TransformedBbox(tight_bbox_raw,
131+
fig.transFigure.inverted())
121132

122133
row1, col1 = divmod(num1, cols)
123134

124-
125135
if num2 is None:
126136
# left
127-
hspaces[row1 * (cols+1) + col1].append(_get_left(tight_bbox, ax_bbox))
137+
hspaces[row1 * (cols + 1) + col1].append(
138+
_get_left(tight_bbox, ax_bbox))
128139
# right
129-
hspaces[row1 * (cols+1) + (col1+1)].append(_get_right(tight_bbox, ax_bbox))
140+
hspaces[row1 * (cols + 1) + (col1 + 1)].append(
141+
_get_right(tight_bbox, ax_bbox))
130142
# top
131-
vspaces[row1 * cols + col1].append(_get_top(tight_bbox, ax_bbox))
143+
vspaces[row1 * cols + col1].append(
144+
_get_top(tight_bbox, ax_bbox))
132145
# bottom
133-
vspaces[(row1+1) * cols + col1].append(_get_bottom(tight_bbox, ax_bbox))
146+
vspaces[(row1 + 1) * cols + col1].append(
147+
_get_bottom(tight_bbox, ax_bbox))
134148

135149
else:
136150
row2, col2 = divmod(num2, cols)
137151

138-
for row_i in range(row1, row2+1):
152+
for row_i in range(row1, row2 + 1):
139153
# left
140-
hspaces[row_i * (cols+1) + col1].append(_get_left(tight_bbox, ax_bbox))
154+
hspaces[row_i * (cols + 1) + col1].append(
155+
_get_left(tight_bbox, ax_bbox))
141156
# right
142-
hspaces[row_i * (cols+1) + (col2+1)].append(_get_right(tight_bbox, ax_bbox))
143-
for col_i in range(col1, col2+1):
157+
hspaces[row_i * (cols + 1) + (col2 + 1)].append(
158+
_get_right(tight_bbox, ax_bbox))
159+
for col_i in range(col1, col2 + 1):
144160
# top
145-
vspaces[row1 * cols + col_i].append(_get_top(tight_bbox, ax_bbox))
161+
vspaces[row1 * cols + col_i].append(
162+
_get_top(tight_bbox, ax_bbox))
146163
# bottom
147-
vspaces[(row2+1) * cols + col_i].append(_get_bottom(tight_bbox, ax_bbox))
148-
164+
vspaces[(row2 + 1) * cols + col_i].append(
165+
_get_bottom(tight_bbox, ax_bbox))
149166

150167
fig_width_inch, fig_height_inch = fig.get_size_inches()
151168

152169
# margins can be negative for axes with aspect applied. And we
153170
# append + [0] to make minimum margins 0
154171

155172
if not margin_left:
156-
margin_left = max([sum(s) for s in hspaces[::cols+1]] + [0])
173+
margin_left = max([sum(s) for s in hspaces[::cols + 1]] + [0])
157174
margin_left += pad_inches / fig_width_inch
158175

159176
if not margin_right:
160-
margin_right = max([sum(s) for s in hspaces[cols::cols+1]] + [0])
177+
margin_right = max([sum(s) for s in hspaces[cols::cols + 1]] + [0])
161178
margin_right += pad_inches / fig_width_inch
162179

163180
if not margin_top:
164-
margin_top = max([sum(s) for s in vspaces[:cols]] + [0])
181+
margin_top = max([sum(s) for s in vspaces[:cols]] + [0])
165182
margin_top += pad_inches / fig_height_inch
166183

167184
if not margin_bottom:
168-
margin_bottom = max([sum(s) for s in vspaces[-cols:]] + [0])
185+
margin_bottom = max([sum(s) for s in vspaces[-cols:]] + [0])
169186
margin_bottom += pad_inches / fig_height_inch
170187

171-
172188
kwargs = dict(left=margin_left,
173-
right=1-margin_right,
189+
right=1 - margin_right,
174190
bottom=margin_bottom,
175-
top=1-margin_top)
191+
top=1 - margin_top)
176192

177193
if cols > 1:
178-
hspace = max([sum(s) for i in range(rows) for s in hspaces[i*(cols+1)+1:(i+1)*(cols+1)-1]])
194+
hspace_strip = hspaces[i * (cols + 1) + 1:(i + 1) * (cols + 1) - 1]
195+
hspace = max([sum(s) for i in range(rows) for s in hspace_strip])
179196
hspace += hpad_inches / fig_width_inch
180-
h_axes = ((1-margin_right-margin_left) - hspace * (cols - 1))/cols
197+
h_axes = ((1 - margin_right - margin_left) -
198+
hspace * (cols - 1)) / cols
181199

182-
kwargs["wspace"]=hspace/h_axes
200+
kwargs["wspace"] = hspace / h_axes
183201

184202
if rows > 1:
185-
vspace = max([sum(s) for s in vspaces[cols:-cols]])
203+
vspace = max([sum(s) for s in vspaces[cols:-cols]])
186204
vspace += vpad_inches / fig_height_inch
187-
v_axes = ((1-margin_top-margin_bottom) - vspace * (rows - 1))/rows
205+
v_axes = ((1 - margin_top - margin_bottom) -
206+
vspace * (rows - 1)) / rows
188207

189-
kwargs["hspace"]=vspace/v_axes
208+
kwargs["hspace"] = vspace / v_axes
190209

191210
return kwargs
192211

@@ -237,7 +256,6 @@ def get_subplotspec_list(axes_list, grid_spec=None):
237256
else:
238257
subplotspec = None
239258

240-
241259
subplotspec_list.append(subplotspec)
242260

243261
return subplotspec_list
@@ -274,15 +292,14 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
274292
labels) will fit into. Default is (0, 0, 1, 1).
275293
"""
276294

277-
278295
subplot_list = []
279296
nrows_list = []
280297
ncols_list = []
281298
ax_bbox_list = []
282299

283-
subplot_dict = {} # multiple axes can share
284-
# same subplot_interface (e.g, axes_grid1). Thus
285-
# we need to join them together.
300+
subplot_dict = {} # multiple axes can share
301+
# same subplot_interface (e.g, axes_grid1). Thus
302+
# we need to join them together.
286303

287304
subplotspec_list2 = []
288305

@@ -314,15 +331,16 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
314331
if (mod_row != 0) or (mod_col != 0):
315332
raise RuntimeError("")
316333

317-
rowNum1, colNum1 = divmod(num1, cols)
334+
rowNum1, colNum1 = divmod(num1, cols)
318335
if num2 is None:
319-
rowNum2, colNum2 = rowNum1, colNum1
336+
rowNum2, colNum2 = rowNum1, colNum1
320337
else:
321-
rowNum2, colNum2 = divmod(num2, cols)
322-
323-
num1num2_list.append((rowNum1*div_row*max_ncols + colNum1*div_col,
324-
((rowNum2+1)*div_row-1)*max_ncols + (colNum2+1)*div_col-1))
338+
rowNum2, colNum2 = divmod(num2, cols)
325339

340+
num1num2_list.append((rowNum1 * div_row * max_ncols +
341+
colNum1 * div_col,
342+
((rowNum2 + 1) * div_row - 1) * max_ncols +
343+
(colNum2 + 1) * div_col - 1))
326344

327345
kwargs = auto_adjust_subplotpars(fig, renderer,
328346
nrows_ncols=(max_nrows, max_ncols),
@@ -341,10 +359,14 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
341359
# with adjusted rect parameters.
342360

343361
left, bottom, right, top = rect
344-
if left is not None: left += kwargs["left"]
345-
if bottom is not None: bottom += kwargs["bottom"]
346-
if right is not None: right -= (1 - kwargs["right"])
347-
if top is not None: top -= (1 - kwargs["top"])
362+
if left is not None:
363+
left += kwargs["left"]
364+
if bottom is not None:
365+
bottom += kwargs["bottom"]
366+
if right is not None:
367+
right -= (1 - kwargs["right"])
368+
if top is not None:
369+
top -= (1 - kwargs["top"])
348370

349371
#if h_pad is None: h_pad = pad
350372
#if w_pad is None: w_pad = pad

0 commit comments

Comments
 (0)