matplotlib/matplotlib · error · ValueError

Cannot convert {b!r} to bool

Error message

Cannot convert {b!r} to bool

What it means

validate_bool converts rcParams boolean values. Accepted truthy: 't', 'y', 'yes', 'on', 'true', '1' (case-insensitive via .lower()), int 1, True. Accepted falsy: 'f', 'n', 'no', 'off', 'false', '0', int 0, False. Everything else — other words, ints like 2, floats like 1.0 — raises ValueError('Cannot convert {b!r} to bool').

Source

Thrown at lib/matplotlib/rcsetup.py:188

def _validate_date(s):
    try:
        np.datetime64(s)
        return s
    except ValueError:
        raise ValueError(
            f'{s!r} should be a string that can be parsed by numpy.datetime64')


def validate_bool(b):
    """Convert b to ``bool`` or raise."""
    if isinstance(b, str):
        b = b.lower()
    if b in ('t', 'y', 'yes', 'on', 'true', '1', 1, True):
        return True
    elif b in ('f', 'n', 'no', 'off', 'false', '0', 0, False):
        return False
    else:
        raise ValueError(f'Cannot convert {b!r} to bool')


def validate_axisbelow(s):
    try:
        return validate_bool(s)
    except ValueError:
        if isinstance(s, str):
            if s == 'line':
                return 'line'
    raise ValueError(f'{s!r} cannot be interpreted as'
                     ' True, False, or "line"')


def validate_dpi(s):
    """Confirm s is string 'figure' or convert s to float or raise."""
    if s == 'figure':
        return s
    try:

View on GitHub (pinned to b379c1b69e)

Solutions

  1. Use one of the accepted literals: true/false, yes/no, on/off, 1/0, t/f, y/n
  2. In code prefer real Python bools: rcParams['savefig.transparent'] = True
  3. Normalize loaded config values: map truthy words/numbers to Python bool before assignment

Example fix

# before
plt.rcParams['figure.autolayout'] = 'enable'  # ValueError

# after
plt.rcParams['figure.autolayout'] = True  # or 'true', 'yes', 'on', '1'
Defensive patterns

Strategy: validation

Validate before calling

TRUTHY = {'t', 'y', 'yes', 'on', 'true', '1', 1, True}
FALSY = {'f', 'n', 'no', 'off', 'false', '0', 0, False}
value = value.lower().strip() if isinstance(value, str) else value
if value not in TRUTHY and value not in FALSY:
    raise ValueError(f'unrecognized boolean {value!r}')
plt.rcParams['figure.autolayout'] = value

Try / catch

try:
    plt.rcParams[key] = raw
except ValueError as e:
    if 'to bool' in str(e):
        plt.rcParams[key] = str(raw).strip().lower() in ('1', 'true', 'yes', 'on', 't', 'y')
    else:
        raise

Prevention

When it happens

Trigger: rcParams['savefig.transparent'] = 'enable' or 'enabled'; rcParams['figure.autolayout'] = 1.0 (float, not accepted); passing 2 or '2'; strings with stray spaces like ' true' (no stripping is done).

Common situations: Descriptive on/off words from other tools' configs; numeric booleans from JSON/YAML that arrive as floats or non-0/1 ints; trailing whitespace in hand-edited matplotlibrc values.

Related errors


AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21). Data as JSON: /api/errors/73d38e4c8819ade0. Report an issue: GitHub.