{"record":{"id":"6dbc070c26481756","repo":"matplotlib/matplotlib","slug":"there-are-duplicate-keys-overlap-between-the-out","errorCode":null,"errorMessage":"There are duplicate keys {overlap} between the outer layout\n{mosaic!r}\nand the nested layout\n{nested_mosaic}","messagePattern":"There are duplicate keys (.+?) between the outer layout\n(.+?)\nand the nested layout\n(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/figure.py","lineNumber":2266,"sourceCode":"                            'label': str(name),\n                            **subplot_kw,\n                            **per_subplot_kw.get(name, {})\n                        }\n                    )\n                    output[name] = ax\n                elif method == 'nested':\n                    nested_mosaic = arg\n                    j, k = key\n                    # recursively add the nested mosaic\n                    rows, cols = nested_mosaic.shape\n                    nested_output = _do_layout(\n                        gs[j, k].subgridspec(rows, cols),\n                        nested_mosaic,\n                        *_identify_keys_and_nested(nested_mosaic)\n                    )\n                    overlap = set(output) & set(nested_output)\n                    if overlap:\n                        raise ValueError(\n                            f\"There are duplicate keys {overlap} \"\n                            f\"between the outer layout\\n{mosaic!r}\\n\"\n                            f\"and the nested layout\\n{nested_mosaic}\"\n                        )\n                    output.update(nested_output)\n                else:\n                    raise RuntimeError(\"This should never happen\")\n            return output\n\n        mosaic = _make_array(mosaic)\n        rows, cols = mosaic.shape\n        gs = self.add_gridspec(rows, cols, **gridspec_kw)\n        ret = _do_layout(gs, mosaic, *_identify_keys_and_nested(mosaic))\n        ax0 = next(iter(ret.values()))\n        for ax in ret.values():\n            if sharex:\n                ax.sharex(ax0)\n                ax._label_outer_xaxis(skip_non_rectangular_axes=True)","sourceCodeStart":2248,"sourceCodeEnd":2284,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/figure.py#L2248-L2284","documentation":"Nested sub-mosaics are flattened into the single dict that subplot_mosaic returns. If a label is used both in the outer layout and inside a nested layout, the keys collide in that flat dict, so _do_layout raises this ValueError listing the overlapping labels (lib/matplotlib/figure.py:2266).","triggerScenarios":"fig.subplot_mosaic([['a', [['a', 'b'], ['c', 'd']]]]) — outer 'a' also appears inside the nested grid; nested layouts generated with generic names like 'plot' or 'cax' that repeat outer names.","commonSituations":"Copying nested layout templates that reuse common label names; composing layouts from helper functions that each use their own fixed labels; refactoring a flat mosaic into nested sub-layouts without renaming.","solutions":["Rename labels inside nested mosaics so every label is globally unique","After creation, remap the returned dict to whatever names your code needs","When generating nested layouts, prefix nested labels with their position or path"],"exampleFix":"// before\nfig.subplot_mosaic([['a', [['a', 'b'], ['c', 'd']]]])\n\n// after\nfig.subplot_mosaic([['a', [['x', 'b'], ['c', 'd']]]])","handlingStrategy":"validation","validationCode":"def nested_labels_unique(mosaic):\n    top, nested = set(), set()\n    def walk(m, depth):\n        for row in m:\n            for v in row:\n                if isinstance(v, (list, tuple)):\n                    walk(v, depth + 1)\n                elif v != '.':\n                    (top if depth == 0 else nested).add(v)\n    walk(mosaic, 0)\n    return not (top & nested)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefix nested labels (e.g. 'tl_a') to keep them globally unique","When composing mosaics from helpers, pass a label prefix to each helper"],"tags":["matplotlib","subplot-mosaic","nested-layout","duplicate-keys"],"backgroundTag":"duplicate-mosaic-keys","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}