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

Skip to content

Commit 5154655

Browse files
committed
made Figure.tight_layout take the rect parameter and did some refactoring.
1 parent 2df4a3b commit 5154655

File tree

4 files changed

+137
-68
lines changed

4 files changed

+137
-68
lines changed

lib/matplotlib/figure.py

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,8 +1361,7 @@ def get_tightbbox(self, renderer):
13611361

13621362
return bbox_inches
13631363

1364-
1365-
def tight_layout(self, renderer=None, pad=1.2, h_pad=None, w_pad=None):
1364+
def tight_layout(self, renderer=None, pad=1.2, h_pad=None, w_pad=None, rect=None):
13661365
"""
13671366
Adjust subplot parameters to give specified padding.
13681367
@@ -1374,75 +1373,20 @@ def tight_layout(self, renderer=None, pad=1.2, h_pad=None, w_pad=None):
13741373
*h_pad*, *w_pad* : float
13751374
padding (height/width) between edges of adjacent subplots.
13761375
Defaults to `pad_inches`.
1376+
*rect* : if rect is given, it is interpreted as a rectangle
1377+
(left, bottom, right, top) in the normalized figure
1378+
coordinate that the whole subplots area (including
1379+
labels) will fit into. Default is (0, 0, 1, 1).
13771380
"""
13781381

1379-
from tight_layout import auto_adjust_subplotpars, get_renderer
1382+
from tight_layout import get_renderer, get_tight_layout_figure
13801383

13811384
if renderer is None:
13821385
renderer = get_renderer(self)
13831386

1384-
subplotspec_list = []
1385-
subplot_list = []
1386-
nrows_list = []
1387-
ncols_list = []
1388-
ax_bbox_list = []
1389-
1390-
subplot_dict = {} # for axes_grid1, multiple axes can share
1391-
# same subplot_interface. Thus we need to
1392-
# join them together.
1393-
1394-
for ax in self.axes:
1395-
locator = ax.get_axes_locator()
1396-
if hasattr(locator, "get_subplotspec"):
1397-
subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
1398-
elif hasattr(ax, "get_subplotspec"):
1399-
subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
1400-
else:
1401-
continue
1402-
1403-
if (subplotspec is None) or \
1404-
subplotspec.get_gridspec().locally_modified_subplot_params():
1405-
continue
1406-
1407-
subplots = subplot_dict.setdefault(subplotspec, [])
1408-
1409-
if not subplots:
1410-
myrows, mycols, _, _ = subplotspec.get_geometry()
1411-
nrows_list.append(myrows)
1412-
ncols_list.append(mycols)
1413-
subplotspec_list.append(subplotspec)
1414-
subplot_list.append(subplots)
1415-
ax_bbox_list.append(subplotspec.get_position(self))
1416-
1417-
subplots.append(ax)
1418-
1419-
max_nrows = max(nrows_list)
1420-
max_ncols = max(ncols_list)
1421-
1422-
num1num2_list = []
1423-
for subplotspec in subplotspec_list:
1424-
rows, cols, num1, num2 = subplotspec.get_geometry()
1425-
div_row, mod_row = divmod(max_nrows, rows)
1426-
div_col, mod_col = divmod(max_ncols, cols)
1427-
if (mod_row != 0) or (mod_col != 0):
1428-
raise RuntimeError("")
1429-
1430-
rowNum1, colNum1 = divmod(num1, cols)
1431-
if num2 is None:
1432-
rowNum2, colNum2 = rowNum1, colNum1
1433-
else:
1434-
rowNum2, colNum2 = divmod(num2, cols)
1435-
1436-
num1num2_list.append((rowNum1*div_row*max_ncols + colNum1*div_col,
1437-
((rowNum2+1)*div_row-1)*max_ncols + (colNum2+1)*div_col-1))
1438-
1439-
1440-
kwargs = auto_adjust_subplotpars(self, renderer,
1441-
nrows_ncols=(max_nrows, max_ncols),
1442-
num1num2_list=num1num2_list,
1443-
subplot_list=subplot_list,
1444-
ax_bbox_list=ax_bbox_list,
1445-
pad=pad, h_pad=h_pad, w_pad=w_pad)
1387+
kwargs = get_tight_layout_figure(self, self.axes, renderer,
1388+
pad=pad, h_pad=h_pad, w_pad=w_pad,
1389+
rect=rect)
14461390

14471391
self.subplots_adjust(**kwargs)
14481392

lib/matplotlib/gridspec.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,16 @@ def tight_layout(self, fig, renderer=None, pad=1.2, h_pad=None, w_pad=None, rect
275275
Adjust subplot parameters to give specified padding.
276276
277277
Parameters:
278-
278+
279279
pad : float
280280
padding between the figure edge and the edges of subplots, as a fraction of the font-size.
281281
h_pad, w_pad : float
282282
padding (height/width) between edges of adjacent subplots.
283283
Defaults to `pad_inches`.
284+
rect : if rect is given, it is interpreted as a rectangle
285+
(left, bottom, right, top) in the normalized figure
286+
coordinate that the whole subplots area (including
287+
labels) will fit into. Default is (0, 0, 1, 1).
284288
"""
285289

286290
from tight_layout import auto_adjust_subplotpars, get_renderer

lib/matplotlib/pyplot.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ def subplot_tool(targetfig=None):
11141114

11151115

11161116

1117-
def tight_layout(pad=1.2, h_pad=None, w_pad=None):
1117+
def tight_layout(pad=1.2, h_pad=None, w_pad=None, rect=None):
11181118
"""
11191119
Adjust subplot parameters to give specified padding.
11201120
@@ -1125,10 +1125,14 @@ def tight_layout(pad=1.2, h_pad=None, w_pad=None):
11251125
h_pad, w_pad : float
11261126
padding (height/width) between edges of adjacent subplots.
11271127
Defaults to `pad_inches`.
1128+
rect : if rect is given, it is interpreted as a rectangle
1129+
(left, bottom, right, top) in the normalized figure
1130+
coordinate that the whole subplots area (including
1131+
labels) will fit into. Default is (0, 0, 1, 1).
11281132
"""
11291133

11301134
fig = gcf()
1131-
fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad)
1135+
fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
11321136
draw_if_interactive()
11331137

11341138

lib/matplotlib/tight_layout.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,120 @@ def get_renderer(fig):
207207
renderer = canvas.get_renderer()
208208

209209
return renderer
210+
211+
212+
def get_tight_layout_figure(fig, axes_list, renderer,
213+
pad=1.2, h_pad=None, w_pad=None, rect=None):
214+
"""
215+
return subplot parameters for tigh-layouted- figure with
216+
specified padding.
217+
218+
Parameters:
219+
220+
*fig* : figure instance
221+
*axes_list* : a list of axes
222+
*renderer* : renderer instance
223+
*pad* : float
224+
padding between the figure edge and the edges of subplots,
225+
as a fraction of the font-size.
226+
*h_pad*, *w_pad* : float
227+
padding (height/width) between edges of adjacent subplots.
228+
Defaults to `pad_inches`.
229+
*rect* : if rect is given, it is interpreted as a rectangle
230+
(left, bottom, right, top) in the normalized figure
231+
coordinate that the whole subplots area (including
232+
labels) will fit into. Default is (0, 0, 1, 1).
233+
"""
234+
235+
236+
subplotspec_list = []
237+
subplot_list = []
238+
nrows_list = []
239+
ncols_list = []
240+
ax_bbox_list = []
241+
242+
subplot_dict = {} # for axes_grid1, multiple axes can share
243+
# same subplot_interface. Thus we need to
244+
# join them together.
245+
246+
for ax in axes_list:
247+
locator = ax.get_axes_locator()
248+
if hasattr(locator, "get_subplotspec"):
249+
subplotspec = locator.get_subplotspec().get_topmost_subplotspec()
250+
elif hasattr(ax, "get_subplotspec"):
251+
subplotspec = ax.get_subplotspec().get_topmost_subplotspec()
252+
else:
253+
continue
254+
255+
if (subplotspec is None) or \
256+
subplotspec.get_gridspec().locally_modified_subplot_params():
257+
continue
258+
259+
subplots = subplot_dict.setdefault(subplotspec, [])
260+
261+
if not subplots:
262+
myrows, mycols, _, _ = subplotspec.get_geometry()
263+
nrows_list.append(myrows)
264+
ncols_list.append(mycols)
265+
subplotspec_list.append(subplotspec)
266+
subplot_list.append(subplots)
267+
ax_bbox_list.append(subplotspec.get_position(fig))
268+
269+
subplots.append(ax)
270+
271+
max_nrows = max(nrows_list)
272+
max_ncols = max(ncols_list)
273+
274+
num1num2_list = []
275+
for subplotspec in subplotspec_list:
276+
rows, cols, num1, num2 = subplotspec.get_geometry()
277+
div_row, mod_row = divmod(max_nrows, rows)
278+
div_col, mod_col = divmod(max_ncols, cols)
279+
if (mod_row != 0) or (mod_col != 0):
280+
raise RuntimeError("")
281+
282+
rowNum1, colNum1 = divmod(num1, cols)
283+
if num2 is None:
284+
rowNum2, colNum2 = rowNum1, colNum1
285+
else:
286+
rowNum2, colNum2 = divmod(num2, cols)
287+
288+
num1num2_list.append((rowNum1*div_row*max_ncols + colNum1*div_col,
289+
((rowNum2+1)*div_row-1)*max_ncols + (colNum2+1)*div_col-1))
290+
291+
292+
kwargs = auto_adjust_subplotpars(fig, renderer,
293+
nrows_ncols=(max_nrows, max_ncols),
294+
num1num2_list=num1num2_list,
295+
subplot_list=subplot_list,
296+
ax_bbox_list=ax_bbox_list,
297+
pad=pad, h_pad=h_pad, w_pad=w_pad)
298+
299+
if rect is not None:
300+
# if rect is given, the whole subplots area (including
301+
# labels) will fit into the rect instead of the
302+
# figure. Note that the rect argument of
303+
# *auto_adjust_subplotpars* specify the area that will be
304+
# covered by the total area of axes.bbox. Thus we call
305+
# auto_adjust_subplotpars twice, where the second run
306+
# with adjusted rect parameters.
307+
308+
left, bottom, right, top = rect
309+
if left is not None: left += kwargs["left"]
310+
if bottom is not None: bottom += kwargs["bottom"]
311+
if right is not None: right -= (1 - kwargs["right"])
312+
if top is not None: top -= (1 - kwargs["top"])
313+
314+
#if h_pad is None: h_pad = pad
315+
#if w_pad is None: w_pad = pad
316+
317+
kwargs = auto_adjust_subplotpars(fig, renderer,
318+
nrows_ncols=(max_nrows, max_ncols),
319+
num1num2_list=num1num2_list,
320+
subplot_list=subplot_list,
321+
ax_bbox_list=ax_bbox_list,
322+
pad=pad, h_pad=h_pad, w_pad=w_pad,
323+
rect=(left, bottom, right, top))
324+
325+
return kwargs
326+

0 commit comments

Comments
 (0)