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

Skip to content

Commit 34454a6

Browse files
committed
add rc parameters for tight_layout parameters
To preserve backwards compatibility, calling fig.tight_layout(pad=pad) will set h_pad=w_pad=pad. All three paddings default to 1.08 (in units of font.size).
1 parent 9e79b0a commit 34454a6

File tree

5 files changed

+45
-25
lines changed

5 files changed

+45
-25
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ def get_tightbbox(self, renderer):
16911691

16921692
return bbox_inches
16931693

1694-
def tight_layout(self, renderer=None, pad=1.08, h_pad=None,
1694+
def tight_layout(self, renderer=None, pad=None, h_pad=None,
16951695
w_pad=None, rect=None):
16961696
"""
16971697
Adjust subplot parameters to give specified padding.
@@ -1701,9 +1701,11 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None,
17011701
*pad* : float
17021702
padding between the figure edge and the edges of subplots,
17031703
as a fraction of the font-size.
1704+
Defaults to rc ``figure.autolayout.pad``.
17041705
*h_pad*, *w_pad* : float
17051706
padding (height/width) between edges of adjacent subplots.
1706-
Defaults to `pad_inches`.
1707+
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
1708+
``figure.autolayout.wpad``.
17071709
*rect* : if rect is given, it is interpreted as a rectangle
17081710
(left, bottom, right, top) in the normalized figure
17091711
coordinate that the whole subplots area (including

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,17 +1358,18 @@ def subplot_tool(targetfig=None):
13581358
return ret
13591359

13601360

1361-
def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
1361+
def tight_layout(pad=None, h_pad=None, w_pad=None, rect=None):
13621362
"""
13631363
Automatically adjust subplot parameters to give specified padding.
13641364
13651365
Parameters:
13661366
13671367
pad : float
13681368
padding between the figure edge and the edges of subplots, as a fraction of the font-size.
1369+
Defaults to rc ``figure.autolayout.pad``.
13691370
h_pad, w_pad : float
13701371
padding (height/width) between edges of adjacent subplots.
1371-
Defaults to `pad_inches`.
1372+
Defaults to `pad` if given or rc ``figure.autolayout.hpad``, ``figure.autolayout.wpad``.
13721373
rect : if rect is given, it is interpreted as a rectangle
13731374
(left, bottom, right, top) in the normalized figure
13741375
coordinate that the whole subplots area (including

lib/matplotlib/rcsetup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,13 @@ def validate_cycler(s):
10471047
'figure.facecolor': ['0.75', validate_color], # facecolor; scalar gray
10481048
'figure.edgecolor': ['w', validate_color], # edgecolor; white
10491049
'figure.frameon': [True, validate_bool],
1050-
'figure.autolayout': [False, validate_bool],
10511050
'figure.max_open_warning': [20, validate_int],
10521051

1052+
'figure.autolayout': [False, validate_bool],
1053+
'figure.autolayout.pad': [1.08, validate_float],
1054+
'figure.autolayout.hpad': [1.08, validate_float],
1055+
'figure.autolayout.wpad': [1.08, validate_float],
1056+
10531057
'figure.subplot.left': [0.125, ValidateInterval(0, 1, closedmin=True,
10541058
closedmax=True)],
10551059
'figure.subplot.right': [0.9, ValidateInterval(0, 1, closedmin=True,

lib/matplotlib/tight_layout.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def auto_adjust_subplotpars(fig, renderer,
3939
num1num2_list,
4040
subplot_list,
4141
ax_bbox_list=None,
42-
pad=1.08, h_pad=None, w_pad=None,
42+
pad=None, h_pad=None, w_pad=None,
4343
rect=None):
4444
"""
4545
Return a dictionary of subplot parameters so that spacing between
@@ -62,29 +62,38 @@ def auto_adjust_subplotpars(fig, renderer,
6262
pad : float
6363
padding between the figure edge and the edges of subplots, as a fraction
6464
of the font-size.
65+
Defaults to rc ``figure.autolayout.pad``.
66+
6567
h_pad, w_pad : float
6668
padding (height/width) between edges of adjacent subplots.
67-
Defaults to `pad_inches`.
69+
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
70+
``figure.autolayout.wpad``.
6871
6972
rect
7073
[left, bottom, right, top] in normalized (0, 1) figure coordinates.
7174
"""
7275
rows, cols = nrows_ncols
7376

74-
pad_inches = pad * FontProperties(
77+
if h_pad is None:
78+
if pad is not None:
79+
h_pad = pad
80+
else:
81+
h_pad = rcParams["figure.autolayout.hpad"]
82+
vpad_inches = h_pad * FontProperties(
7583
size=rcParams["font.size"]).get_size_in_points() / 72.
7684

77-
if h_pad is not None:
78-
vpad_inches = h_pad * FontProperties(
79-
size=rcParams["font.size"]).get_size_in_points() / 72.
80-
else:
81-
vpad_inches = pad_inches
85+
if w_pad is None:
86+
if pad is not None:
87+
w_pad = pad
88+
else:
89+
w_pad = rcParams["figure.autolayout.wpad"]
90+
hpad_inches = w_pad * FontProperties(
91+
size=rcParams["font.size"]).get_size_in_points() / 72.
8292

83-
if w_pad is not None:
84-
hpad_inches = w_pad * FontProperties(
85-
size=rcParams["font.size"]).get_size_in_points() / 72.
86-
else:
87-
hpad_inches = pad_inches
93+
if pad is None:
94+
pad = rcParams["figure.autolayout.pad"]
95+
pad_inches = pad * FontProperties(
96+
size=rcParams["font.size"]).get_size_in_points() / 72.
8897

8998
if len(subplot_list) == 0:
9099
raise RuntimeError("")
@@ -261,7 +270,7 @@ def get_subplotspec_list(axes_list, grid_spec=None):
261270

262271

263272
def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
264-
pad=1.08, h_pad=None, w_pad=None, rect=None):
273+
pad=None, h_pad=None, w_pad=None, rect=None):
265274
"""
266275
Return subplot parameters for tight-layouted-figure with specified
267276
padding.
@@ -280,10 +289,12 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
280289
*pad* : float
281290
padding between the figure edge and the edges of subplots,
282291
as a fraction of the font-size.
292+
Defaults to rc ``figure.autolayout.pad``.
283293
284294
*h_pad*, *w_pad* : float
285295
padding (height/width) between edges of adjacent subplots.
286-
Defaults to `pad_inches`.
296+
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
297+
``figure.autolayout.wpad``.
287298
288299
*rect* : if rect is given, it is interpreted as a rectangle
289300
(left, bottom, right, top) in the normalized figure
@@ -367,9 +378,6 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
367378
if top is not None:
368379
top -= (1 - kwargs["top"])
369380

370-
#if h_pad is None: h_pad = pad
371-
#if w_pad is None: w_pad = pad
372-
373381
kwargs = auto_adjust_subplotpars(fig, renderer,
374382
nrows_ncols=(max_nrows, max_ncols),
375383
num1num2_list=num1num2_list,

matplotlibrc.template

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,17 @@ backend : %(backend)s
335335
#figure.dpi : 80 # figure dots per inch
336336
#figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
337337
#figure.edgecolor : white # figure edgecolor
338-
#figure.autolayout : False # When True, automatically adjust subplot
339-
# parameters to make the plot fit the figure
340338
#figure.max_open_warning : 20 # The maximum number of figures to open through
341339
# the pyplot interface before emitting a warning.
342340
# If less than one this feature is disabled.
343341

342+
# Figure tight_layout parameters. All dimensions are in units of the font size.
343+
#figure.autolayout : False # When True, automatically adjust subplot
344+
# parameters to make the plot fit the figure
345+
#figure.autolayout.pad : 1.08 # padding between the figure edge and the edges of subplots
346+
#figure.autolayout.hpad : 1.08 # vertical padding between edges of adjacent subplots
347+
#figure.autolayout.wpad : 1.08 # horizontal padding between edges of adjacent subplots
348+
344349
# The figure subplot parameters. All dimensions are a fraction of the
345350
# figure width or height
346351
#figure.subplot.left : 0.125 # the left side of the subplots of the figure

0 commit comments

Comments
 (0)