{"record":{"id":"26cba3cd53137c0d","repo":"matplotlib/matplotlib","slug":"rgrids-only-defined-for-polar-axes","errorCode":null,"errorMessage":"rgrids only defined for polar Axes","messagePattern":"rgrids only defined for polar Axes","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/pyplot.py","lineNumber":2553,"sourceCode":"    --------\n    .pyplot.thetagrids\n    .projections.polar.PolarAxes.set_rgrids\n    .Axis.get_gridlines\n    .Axis.get_ticklabels\n\n    Examples\n    --------\n    ::\n\n      # set the locations of the radial gridlines\n      lines, labels = rgrids( (0.25, 0.5, 1.0) )\n\n      # set the locations and labels of the radial gridlines\n      lines, labels = rgrids( (0.25, 0.5, 1.0), ('Tom', 'Dick', 'Harry' ))\n    \"\"\"\n    ax = gca()\n    if not isinstance(ax, PolarAxes):\n        raise RuntimeError('rgrids only defined for polar Axes')\n    if all(p is None for p in [radii, labels, angle, fmt]) and not kwargs:\n        lines_out: list[Line2D] = ax.yaxis.get_gridlines()\n        labels_out: list[Text] = ax.yaxis.get_ticklabels()\n    elif radii is None:\n        raise TypeError(\"'radii' cannot be None when other parameters are passed\")\n    else:\n        lines_out, labels_out = ax.set_rgrids(\n            radii, labels=labels, angle=angle, fmt=fmt, **kwargs)\n    return lines_out, labels_out\n\n\ndef thetagrids(\n    angles: ArrayLike | None = None,\n    labels: Sequence[str | Text] | None = None,\n    fmt: str | None = None,\n    **kwargs\n) -> tuple[list[Line2D], list[Text]]:\n    \"\"\"","sourceCodeStart":2535,"sourceCodeEnd":2571,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/pyplot.py#L2535-L2571","documentation":"plt.rgrids() configures radial gridlines of a polar plot, but it operates on the CURRENT axes via gca() and checks the type: if the current axes is not a PolarAxes it raises RuntimeError('rgrids only defined for polar Axes'). After any cartesian plot (or at session start with a default Axes), the current axes is cartesian, so calling rgrids() blindly fails.","triggerScenarios":"plt.plot(x, y); plt.rgrids((0.25, 0.5, 1.0)); calling rgrids as the first pyplot command (gca() creates a default cartesian axes); plotting on a cartesian figure, then creating a polar axes that is not current, then calling rgrids.","commonSituations":"Copy-pasting a polar-grid snippet into a notebook whose current axes is cartesian; ordering bugs where rgrids runs before plt.subplot(projection='polar'); mixed figure layouts where the polar axes lost focus.","solutions":["Make the polar axes current first: plt.subplot(projection='polar') immediately before plt.rgrids(...)","Better, avoid current-axes coupling: ax = plt.subplot(projection='polar'); ax.set_rgrids((0.25, 0.5, 1.0), ...)","Check isinstance(plt.gca(), PolarAxes) before calling rgrids in shared code"],"exampleFix":"# before\nplt.plot([1, 2], [3, 4])\nplt.rgrids((0.25, 0.5, 1.0))  # RuntimeError: not polar\n\n# after\nax = plt.subplot(projection='polar')\nax.set_rgrids((0.25, 0.5, 1.0))","handlingStrategy":"type-guard","validationCode":"import matplotlib.pyplot as plt\nfrom matplotlib.projections.polar import PolarAxes\n\ndef rgrids_or_none(*args, **kwargs):\n    if not isinstance(plt.gca(), PolarAxes):\n        return None  # nothing to configure on cartesian axes\n    return plt.rgrids(*args, **kwargs)","typeGuard":"from matplotlib.projections.polar import PolarAxes\nfrom matplotlib.axes import Axes\n\ndef is_polar(ax: Axes) -> bool:\n    \"\"\"True when ax supports rgrids/set_rgrids (radial gridlines).\"\"\"\n    return isinstance(ax, PolarAxes)","tryCatchPattern":null,"preventionTips":["Create the polar axes first (plt.subplot(projection='polar')) before polar-only pyplot helpers","Prefer ax.set_rgrids(...) on a held PolarAxes reference over pyplot functions that use gca()","Type-check gca() before polar-only calls in generic styling code"],"tags":["matplotlib","pyplot","rgrids","polar","wrong-axes-type","runtimeerror"],"backgroundTag":"axes-projection-mismatch","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}