Skip to content

Commit

Permalink
MNT: Error out if plt.polar() is called with an existing non-polar Axes
Browse files Browse the repository at this point in the history
Before, we only issued a warning. But we clearly cannot fulfill the intention
of a polar plot and therefore we should error out instead of just warn.

Also document that `polar()` should typically be called first to prevent having
a non-polar Axes already created.
  • Loading branch information
timhoffm committed Oct 7, 2024
1 parent 60458da commit 7edef3a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,17 +2686,28 @@ def polar(*args, **kwargs) -> list[Line2D]:
call signature::
polar(theta, r, **kwargs)
Multiple *theta*, *r* arguments are supported, with format strings, as in
`plot`.
polar(theta, r, [fmt], **kwargs)
This is a convenience wrapper around `.pyplot.plot`. It ensures that the
current Axes is polar (or creates one if needed) and then passes all parameters
to ``.pyplot.plot``.
.. note::
When making polar plots using the :ref:`pyplot API <pyplot_interface>`,
``polar()`` should typically be the first command because that makes sure
a polar Axes is created. Using other commands such as ``plt.title()``
before can lead to the implicit creation of a rectangular Axes, in which
case a subsequent ``polar()`` call will fail.
"""
# If an axis already exists, check if it has a polar projection
if gcf().get_axes():
ax = gca()
if not isinstance(ax, PolarAxes):
_api.warn_external('Trying to create polar plot on an Axes '
'that does not have a polar projection.')
raise RuntimeError(
"There exists a non-polar current Axes. 'polar()' cannot plot "
"into this. You likely should to call 'polar()' before other "
"plotting commands."
)
else:
ax = axes(projection="polar")
return ax.plot(*args, **kwargs)
Expand Down

0 comments on commit 7edef3a

Please sign in to comment.