{"record":{"id":"f9304ffc82077757","repo":"matplotlib/matplotlib","slug":"invalid-value-for-layout-layout-r","errorCode":null,"errorMessage":"Invalid value for 'layout': {layout!r}","messagePattern":"Invalid value for 'layout': (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/figure.py","lineNumber":2825,"sourceCode":"        if layout == 'tight':\n            new_layout_engine = TightLayoutEngine(**kwargs)\n        elif layout == 'constrained':\n            new_layout_engine = ConstrainedLayoutEngine(**kwargs)\n        elif layout == 'compressed':\n            new_layout_engine = ConstrainedLayoutEngine(compress=True,\n                                                        **kwargs)\n        elif layout == 'none':\n            if self._layout_engine is not None:\n                new_layout_engine = PlaceHolderLayoutEngine(\n                    self._layout_engine.adjust_compatible,\n                    self._layout_engine.colorbar_gridspec\n                )\n            else:\n                new_layout_engine = None\n        elif isinstance(layout, LayoutEngine):\n            new_layout_engine = layout\n        else:\n            raise ValueError(f\"Invalid value for 'layout': {layout!r}\")\n\n        if self._check_layout_engines_compat(self._layout_engine,\n                                             new_layout_engine):\n            self._layout_engine = new_layout_engine\n        else:\n            raise RuntimeError('Colorbar layout of new layout engine not '\n                               'compatible with old engine, and a colorbar '\n                               'has been created.  Engine not changed.')\n\n    def get_layout_engine(self):\n        return self._layout_engine\n\n    # TODO: I'd like to dynamically add the _repr_html_ method\n    # to the figure in the right context, but then IPython doesn't\n    # use it, for some reason.\n\n    def _repr_html_(self):\n        # We can't use \"isinstance\" here, because then we'd end up importing","sourceCodeStart":2807,"sourceCodeEnd":2843,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/figure.py#L2807-L2843","documentation":"set_layout_engine — reached from Figure(layout=...), plt.figure(layout=...), or fig.set_layout_engine(...) — accepts only the strings 'constrained', 'compressed', 'tight', 'none', None, or a LayoutEngine instance. Any other value falls through the dispatch chain to this ValueError (lib/matplotlib/figure.py:2825).","triggerScenarios":"plt.figure(layout='constrained ') with a trailing space; layout='Tight' (wrong case); layout='auto' or 'default' (not real options); passing TightLayoutEngine (the class) instead of TightLayoutEngine().","commonSituations":"Typos and case errors in user-supplied config; forwarding unvalidated layout values from YAML/JSON settings; version drift where layout names got mangled between releases or docs.","solutions":["Use one of the exact strings: 'constrained', 'compressed', 'tight', 'none'","For custom engines pass an instance: fig.set_layout_engine(TightLayoutEngine())","Normalize config values first: layout = str(layout).strip().lower() and validate against the allowed set"],"exampleFix":"// before\nfig = plt.figure(layout='constrained ')\n\n// after\nlayout = 'constrained'\nassert layout in {'constrained', 'compressed', 'tight', 'none'}\nfig = plt.figure(layout=layout)","handlingStrategy":"validation","validationCode":"from matplotlib.layout_engine import LayoutEngine\n\nALLOWED_LAYOUTS = {'constrained', 'compressed', 'tight', 'none'}\n\ndef valid_layout(layout):\n    return layout is None or layout in ALLOWED_LAYOUTS \\\n        or isinstance(layout, LayoutEngine)","typeGuard":"from matplotlib.layout_engine import LayoutEngine\n\ndef is_layout_engine(x):\n    return isinstance(x, LayoutEngine)","tryCatchPattern":null,"preventionTips":["Normalize config strings: str(layout).strip().lower() before use","Validate layout against the allowed set at config load time","Pass engine instances, not classes, for custom engines"],"tags":["matplotlib","layout","layout-engine","invalid-value"],"backgroundTag":"invalid-layout-value","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}