8000 Fix invalid range validators. · matplotlib/matplotlib@f09e9c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f09e9c5

Browse files
committed
Fix invalid range validators.
- The correct bound on margins value, documented in set_x/y/zmargin, is margin>-0.5. Fix the docstring of margins() and the rc validators. - The only constraints on tight/constrained layout margins is left<right and bottom<top, but values outside of [0, 1] can in fact be valid. It would be annoying to enforce this at the rcparams level (this would force the user to update individual rcparams in a careful order), so just don't bother validating the values beyond "float". - Remove the somewhat overengineered _range_validators. - Move the comment about the values of figure.constrained_layout.h/w_pad to the default matplotlibrc (in rcsetup.py it was far away from the actual values, which made things a bit weird). Also reword a bit.
1 parent efd66d4 commit f09e9c5

File tree

4 files changed

+26
-28
lines changed
  • 4 files changed

    +26
    -28
    lines changed

    lib/matplotlib/_constrained_layout.py

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -678,7 +678,7 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None):
    678678
    width and height padding (in fraction of figure)
    679679
    hspace, wspace : float
    680680
    width and height padding as fraction of figure size divided by
    681-
    number of columns or rows
    681+
    number of columns or rows
    682682
    margin : array-like
    683683
    offset the colorbar needs to be pushed to in order to
    684684
    account for multiple colorbars

    lib/matplotlib/axes/_base.py

    Lines changed: 2 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2665,10 +2665,10 @@ def margins(self, *margins, x=None, y=None, tight=True):
    26652665
    26662666
    The padding added to each limit of the Axes is the *margin*
    26672667
    times the data interval. All input parameters must be floats
    2668-
    within the range [0, 1]. Passing both positional and keyword
    2668+
    greater than -0.5. Passing both positional and keyword
    26692669
    arguments is invalid and will raise a TypeError. If no
    26702670
    arguments (positional or otherwise) are provided, the current
    2671-
    margins will remain in place and simply be returned.
    2671+
    margins will remain unchanged and simply be returned.
    26722672
    26732673
    Specifying any margin changes only the autoscaling; for example,
    26742674
    if *xmargin* is not None, then *xmargin* times the X data

    lib/matplotlib/mpl-data/matplotlibrc

    Lines changed: 8 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -585,10 +585,14 @@
    585585
    #figure.constrained_layout.use: False # When True, automatically make plot
    586586
    # elements fit on the figure. (Not
    587587
    # compatible with `autolayout`, above).
    588-
    #figure.constrained_layout.h_pad: 0.04167 # Padding around axes objects. Float representing
    589-
    #figure.constrained_layout.w_pad: 0.04167 # inches. Default is 3/72 inches (3 points)
    590-
    #figure.constrained_layout.hspace: 0.02 # Space between subplot groups. Float representing
    591-
    #figure.constrained_layout.wspace: 0.02 # a fraction of the subplot widths being separated.
    588+
    ## Padding (in inches) around axes; defaults to 3/72 inches, i.e. 3 points.
    589+
    #figure.constrained_layout.h_pad: 0.04167
    590+
    #figure.constrained_layout.w_pad: 0.04167
    591+
    ## Spacing between subplots, relative to the subplot sizes. Much smaller than for
    592+
    ## tight_layout (figure.subplot.hspace, figure.subplot.wspace) as constrained_layout
    593+
    ## already takes surrounding texts (titles, labels, # ticklabels) into account.
    594+
    #figure.constrained_layout.hspace: 0.02
    595+
    #figure.constrained_layout.wspace: 0.02
    592596

    593597

    594598
    ## ***************************************************************************

    lib/matplotlib/rcsetup.py

    Lines changed: 15 additions & 21 deletions
    Original file line numberDiff line numberDiff line change
    @@ -552,12 +552,12 @@ def validate_sketch(s):
    552552
    raise ValueError("Expected a (scale, length, randomness) triplet")
    553553

    554554

    555-
    def _validate_greaterequal0_lessthan1(s):
    555+
    def _validate_greaterthan_minushalf(s):
    556556
    s = validate_float(s)
    557-
    if 0 <= s < 1:
    557+
    if s > -0.5:
    558558
    return s
    559559
    else:
    560-
    raise RuntimeError(f'Value must be >=0 and <1; got {s}')
    560+
    raise RuntimeError(f'Value must be >-0.5; got {s}')
    561561

    562562

    563563
    def _validate_greaterequal0_lessequal1(s):
    @@ -568,12 +568,6 @@ def _validate_greaterequal0_lessequal1(s):
    568568
    raise RuntimeError(f'Value must be >=0 and <=1; got {s}')
    569569

    570570

    571-
    _range_validators = { # Slightly nicer (internal) API.
    572-
    "0 <= x < 1": _validate_greaterequal0_lessthan1,
    573-
    "0 <= x <= 1": _validate_greaterequal0_lessequal1,
    574-
    }
    575-
    576-
    577571
    def validate_hatch(s):
    578572
    r"""
    579573
    Validate a hatch pattern.
    @@ -1012,9 +1006,9 @@ def _convert_validator_spec(key, conv):
    10121006
    # If "data", axes limits are set close to the data.
    10131007
    # If "round_numbers" axes limits are set to the nearest round numbers.
    10141008
    "axes.autolimit_mode": ["data", "round_numbers"],
    1015-
    "axes.xmargin": _range_validators["0 <= x <= 1"], # margin added to xaxis
    1016-
    "axes.ymargin": _range_validators["0 <= x <= 1"], # margin added to yaxis
    1017-
    'axes.zmargin': _range_validators["0 <= x <= 1"], # margin added to zaxis
    1009+
    "axes.xmargin": _validate_greaterthan_minushalf, # margin added to xaxis
    1010+
    "axes.ymargin": _validate_greaterthan_minushalf, # margin added to yaxis
    1011+
    'axes.zmargin': _validate_greaterthan_minushalf, # margin added to zaxis
    10181012

    10191013
    "polaraxes.grid": validate_bool, # display polar grid or not
    10201014
    "axes3d.grid": validate_bool, # display 3d grid
    @@ -1149,18 +1143,18 @@ def _convert_validator_spec(key, conv):
    11491143
    "figure.max_open_warning": validate_int,
    11501144
    "figure.raise_window": validate_bool,
    11511145

    1152-
    "figure.subplot.left": _range_validators["0 <= x <= 1"],
    1153-
    "figure.subplot.right": _range_validators["0 <= x <= 1"],
    1154-
    "figure.subplot.bottom": _range_validators["0 <= x <= 1"],
    1155-
    "figure.subplot.top": _range_validators["0 <= x <= 1"],
    1156-
    "figure.subplot.wspace": _range_validators["0 <= x < 1"],
    1157-
    "figure.subplot.hspace": _range_validators["0 <= x < 1"],
    1146+
    "figure.subplot.left": validate_float,
    1147+
    "figure.subplot.right": validate_float,
    1148+
    "figure.subplot.bottom": validate_float,
    1149+
    "figure.subplot.top": validate_float,
    1150+
    "figure.subplot.wspace": validate_float,
    1151+
    "figure.subplot.hspace": validate_float,
    11581152

    11591153
    "figure.constrained_layout.use": validate_bool, # run constrained_layout?
    11601154
    # wspace and hspace are fraction of adjacent subplots to use for space.
    11611155
    # Much smaller than above because we don't need room for the text.
    1162-
    "figure.constrained_layout.hspace": _range_validators["0 <= x < 1"],
    1163-
    "figure.constrained_layout.wspace": _range_validators["0 <= x < 1"],
    1156+
    "figure.constrained_layout.hspace": validate_float,
    1157+
    "figure.constrained_layout.wspace": validate_float,
    11641158
    # buffer around the axes, in inches.
    11651159
    'figure.constrained_layout.h_pad': validate_float,
    11661160
    'figure.constrained_layout.w_pad': validate_float,
    @@ -1207,7 +1201,7 @@ def _convert_validator_spec(key, conv):
    12071201
    "docstring.hardcopy": validate_bool,
    12081202

    12091203
    "path.simplify": validate_bool,
    1210-
    "path.simplify_threshold": _range_validators["0 <= x <= 1"],
    1204+
    "path.simplify_threshold": _validate_greaterequal0_lessequal1,
    12111205
    "path.snap": validate_bool,
    12121206
    "path.sketch": validate_sketch,
    12131207
    "path.effects": validate_anylist,

    0 commit comments

    Comments
     (0)
    0