Skip to content

Add rc parameters for tight_layout parameters #5271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ def get_tightbbox(self, renderer):

return bbox_inches

def tight_layout(self, renderer=None, pad=1.08, h_pad=None,
def tight_layout(self, renderer=None, pad=None, h_pad=None,
w_pad=None, rect=None):
"""
Adjust subplot parameters to give specified padding.
Expand All @@ -1701,9 +1701,11 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None,
*pad* : float
padding between the figure edge and the edges of subplots,
as a fraction of the font-size.
Defaults to rc ``figure.autolayout.pad``.
*h_pad*, *w_pad* : float
padding (height/width) between edges of adjacent subplots.
Defaults to `pad_inches`.
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
``figure.autolayout.wpad``.
*rect* : if rect is given, it is interpreted as a rectangle
(left, bottom, right, top) in the normalized figure
coordinate that the whole subplots area (including
Expand All @@ -1728,6 +1730,13 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None,
rect=rect)

self.subplots_adjust(**kwargs)
self._tight_parameters.update(
(k, v) for k, v in (
('pad', pad),
('h_pad', h_pad),
('w_pad', w_pad),
('rect', rect),
) if v is not None)


def figaspect(arg):
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,19 @@ def locally_modified_subplot_params(self):
return [k for k in self._AllowedKeys if getattr(self, k)]


def tight_layout(self, fig, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=None):
def tight_layout(self, fig, renderer=None, pad=None, h_pad=None, w_pad=None, rect=None):
"""
Adjust subplot parameters to give specified padding.

Parameters:

pad : float
padding between the figure edge and the edges of subplots, as a fraction of the font-size.
Defaults to rc ``figure.autolayout.pad``.
h_pad, w_pad : float
padding (height/width) between edges of adjacent subplots.
Defaults to `pad_inches`.
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
``figure.autolayout.wpad``.
rect : if rect is given, it is interpreted as a rectangle
(left, bottom, right, top) in the normalized figure
coordinate that the whole subplots area (including
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,17 +1358,18 @@ def subplot_tool(targetfig=None):
return ret


def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
def tight_layout(pad=None, h_pad=None, w_pad=None, rect=None):
"""
Automatically adjust subplot parameters to give specified padding.

Parameters:

pad : float
padding between the figure edge and the edges of subplots, as a fraction of the font-size.
Defaults to rc ``figure.autolayout.pad``.
h_pad, w_pad : float
padding (height/width) between edges of adjacent subplots.
Defaults to `pad_inches`.
Defaults to `pad` if given or rc ``figure.autolayout.hpad``, ``figure.autolayout.wpad``.
rect : if rect is given, it is interpreted as a rectangle
(left, bottom, right, top) in the normalized figure
coordinate that the whole subplots area (including
Expand Down
6 changes: 5 additions & 1 deletion lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,13 @@ def validate_cycler(s):
'figure.facecolor': ['0.75', validate_color], # facecolor; scalar gray
'figure.edgecolor': ['w', validate_color], # edgecolor; white
'figure.frameon': [True, validate_bool],
'figure.autolayout': [False, validate_bool],
'figure.max_open_warning': [20, validate_int],

'figure.autolayout': [False, validate_bool],
'figure.autolayout.pad': [1.08, validate_float],
'figure.autolayout.hpad': [1.08, validate_float],
'figure.autolayout.wpad': [1.08, validate_float],

'figure.subplot.left': [0.125, ValidateInterval(0, 1, closedmin=True,
closedmax=True)],
'figure.subplot.right': [0.9, ValidateInterval(0, 1, closedmin=True,
Expand Down
44 changes: 26 additions & 18 deletions lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def auto_adjust_subplotpars(fig, renderer,
num1num2_list,
subplot_list,
ax_bbox_list=None,
pad=1.08, h_pad=None, w_pad=None,
pad=None, h_pad=None, w_pad=None,
rect=None):
"""
Return a dictionary of subplot parameters so that spacing between
Expand All @@ -62,29 +62,38 @@ def auto_adjust_subplotpars(fig, renderer,
pad : float
padding between the figure edge and the edges of subplots, as a fraction
of the font-size.
Defaults to rc ``figure.autolayout.pad``.

h_pad, w_pad : float
padding (height/width) between edges of adjacent subplots.
Defaults to `pad_inches`.
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
``figure.autolayout.wpad``.

rect
[left, bottom, right, top] in normalized (0, 1) figure coordinates.
"""
rows, cols = nrows_ncols

pad_inches = pad * FontProperties(
if h_pad is None:
if pad is not None:
h_pad = pad
else:
h_pad = rcParams["figure.autolayout.hpad"]
vpad_inches = h_pad * FontProperties(
size=rcParams["font.size"]).get_size_in_points() / 72.

if h_pad is not None:
vpad_inches = h_pad * FontProperties(
size=rcParams["font.size"]).get_size_in_points() / 72.
else:
vpad_inches = pad_inches
if w_pad is None:
if pad is not None:
w_pad = pad
else:
w_pad = rcParams["figure.autolayout.wpad"]
hpad_inches = w_pad * FontProperties(
size=rcParams["font.size"]).get_size_in_points() / 72.

if w_pad is not None:
hpad_inches = w_pad * FontProperties(
size=rcParams["font.size"]).get_size_in_points() / 72.
else:
hpad_inches = pad_inches
if pad is None:
pad = rcParams["figure.autolayout.pad"]
pad_inches = pad * FontProperties(
size=rcParams["font.size"]).get_size_in_points() / 72.

if len(subplot_list) == 0:
raise RuntimeError("")
Expand Down Expand Up @@ -261,7 +270,7 @@ def get_subplotspec_list(axes_list, grid_spec=None):


def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
pad=1.08, h_pad=None, w_pad=None, rect=None):
pad=None, h_pad=None, w_pad=None, rect=None):
"""
Return subplot parameters for tight-layouted-figure with specified
padding.
Expand All @@ -280,10 +289,12 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
*pad* : float
padding between the figure edge and the edges of subplots,
as a fraction of the font-size.
Defaults to rc ``figure.autolayout.pad``.

*h_pad*, *w_pad* : float
padding (height/width) between edges of adjacent subplots.
Defaults to `pad_inches`.
Defaults to `pad` if given or rc ``figure.autolayout.hpad``,
``figure.autolayout.wpad``.

*rect* : if rect is given, it is interpreted as a rectangle
(left, bottom, right, top) in the normalized figure
Expand Down Expand Up @@ -367,9 +378,6 @@ def get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer,
if top is not None:
top -= (1 - kwargs["top"])

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

kwargs = auto_adjust_subplotpars(fig, renderer,
nrows_ncols=(max_nrows, max_ncols),
num1num2_list=num1num2_list,
Expand Down
15 changes: 10 additions & 5 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,17 @@ backend : %(backend)s
#figure.dpi : 80 # figure dots per inch
#figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
#figure.edgecolor : white # figure edgecolor
#figure.autolayout : False # When True, automatically adjust subplot
# parameters to make the plot fit the figure
#figure.max_open_warning : 20 # The maximum number of figures to open through
# the pyplot interface before emitting a warning.
# If less than one this feature is disabled.

# Figure tight_layout parameters. All dimensions are in units of the font size.
#figure.autolayout : False # When True, automatically adjust subplot
# parameters to make the plot fit the figure
#figure.autolayout.pad : 1.08 # padding between the figure edge and the edges of subplots
#figure.autolayout.hpad : 1.08 # vertical padding between edges of adjacent subplots
#figure.autolayout.wpad : 1.08 # horizontal padding between edges of adjacent subplots

# The figure subplot parameters. All dimensions are a fraction of the
# figure width or height
#figure.subplot.left : 0.125 # the left side of the subplots of the figure
Expand All @@ -357,9 +362,9 @@ backend : %(backend)s
#image.lut : 256 # the size of the colormap lookup table
#image.origin : upper # lower | upper
#image.resample : False
#image.composite_image : True # When True, all the images on a set of axes are
# combined into a single composite image before
# saving a figure as a vector graphics file,
#image.composite_image : True # When True, all the images on a set of axes are
# combined into a single composite image before
# saving a figure as a vector graphics file,
# such as a PDF.

### CONTOUR PLOTS
Expand Down