{"record":{"id":"cd024771abb0e6f6","repo":"matplotlib/matplotlib","slug":"autopct-must-be-callable-or-a-format-string","errorCode":null,"errorMessage":"autopct must be callable or a format string","messagePattern":"autopct must be callable or a format string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/axes/_axes.py","lineNumber":3822,"sourceCode":"            # Add labels to the wedges.\n            labels_textprops = {\n                'fontsize': mpl.rcParams['xtick.labelsize'],\n                **cbook.normalize_kwargs(textprops or {}, Text)\n            }\n            self.pie_label(pc, labels, distance=labeldistance,\n                           alignment='outer', rotate=rotatelabels,\n                           textprops=labels_textprops)\n\n        if autopct is not None:\n            # Add automatic percentage labels to wedges\n            auto_labels = []\n            for frac in fracs:\n                if isinstance(autopct, str):\n                    s = autopct % (100. * frac)\n                elif callable(autopct):\n                    s = autopct(100. * frac)\n                else:\n                    raise TypeError(\n                        'autopct must be callable or a format string')\n                if textprops is not None and mpl._val_or_rc(textprops.get(\"usetex\"),\n                                                            \"text.usetex\"):\n                    # escape % (i.e. \\%) if it is not already escaped\n                    s = re.sub(r\"([^\\\\])%\", r\"\\1\\\\%\", s)\n                auto_labels.append(s)\n\n            self.pie_label(pc, auto_labels, distance=pctdistance,\n                           alignment='center',\n                           textprops=textprops)\n\n        if frame:\n            self._request_autoscale_view()\n        else:\n            self.set(frame_on=False, xticks=[], yticks=[],\n                     xlim=(-1.25 + center[0], 1.25 + center[0]),\n                     ylim=(-1.25 + center[1], 1.25 + center[1]))\n","sourceCodeStart":3804,"sourceCodeEnd":3840,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/axes/_axes.py#L3804-L3840","documentation":"Axes.pie raises this TypeError when the autopct argument is neither a string nor a callable. autopct annotates each wedge with its percentage and supports exactly two forms: a printf-style format string (which receives 100*frac) or a function taking the percentage. None is allowed and disables percentage labels.","triggerScenarios":"ax.pie(x, autopct=5) or ax.pie(x, autopct=['10%', '20%', '70%']) - a number or a list of strings is rejected. Also passing a preformatted percent value instead of a template.","commonSituations":"Assuming autopct takes a number (e.g. autopct=1 meaning '1 decimal'); passing one custom string per wedge (that belongs in labels/wedge_labels); copy-pasting examples where autopct was a lambda and replacing it with a value.","solutions":["Use a format string: autopct='%1.1f%%' (the literal percent must be escaped as %%).","Or a callable: autopct=lambda pct: f'{pct:.1f}%'.","If you meant one custom label per wedge, use labels or wedge_labels instead of autopct.","Pass autopct=None to disable percentage labels entirely."],"exampleFix":"# before\nax.pie(x, autopct=['10%', '20%', '70%'])   # list of strings is not accepted\n\n# after\nax.pie(x, autopct='%1.1f%%')               # or autopct=lambda pct: f'{pct:.1f}%'","handlingStrategy":"type-guard","validationCode":"assert autopct is None or isinstance(autopct, str) or callable(autopct), \\\n    'autopct must be a format string or a callable'\nax.pie(x, autopct=autopct)","typeGuard":"def is_valid_autopct(autopct) -> bool:\n    return autopct is None or isinstance(autopct, str) or callable(autopct)","tryCatchPattern":null,"preventionTips":["Remember the two accepted forms: '%1.1f%%' or a lambda over the percentage.","Per-wedge custom text belongs in labels/wedge_labels, not autopct.","Escape literal percent signs as %% inside the format string."],"tags":["matplotlib","pie-chart","type-error","input-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}