Skip to content

Commit fc85a12

Browse files
committed
MNT: Deprecate plt.polar() with an existing non-polar Axes
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.
1 parent 60458da commit fc85a12

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Calling ``pyplot.polar()`` with an existing non-polar Axes
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
This currently plots the data into the non-polar Axes, ignoring
5+
the "polar" intention. This usage scenario is deprecated and
6+
will raise an error in the future.

lib/matplotlib/pyplot.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -2686,17 +2686,32 @@ def polar(*args, **kwargs) -> list[Line2D]:
26862686
26872687
call signature::
26882688
2689-
polar(theta, r, **kwargs)
2690-
2691-
Multiple *theta*, *r* arguments are supported, with format strings, as in
2692-
`plot`.
2689+
polar(theta, r, [fmt], **kwargs)
2690+
2691+
This is a convenience wrapper around `.pyplot.plot`. It ensures that the
2692+
current Axes is polar (or creates one if needed) and then passes all parameters
2693+
to ``.pyplot.plot``.
2694+
2695+
.. note::
2696+
When making polar plots using the :ref:`pyplot API <pyplot_interface>`,
2697+
``polar()`` should typically be the first command because that makes sure
2698+
a polar Axes is created. Using other commands such as ``plt.title()``
2699+
before this can lead to the implicit creation of a rectangular Axes, in which
2700+
case a subsequent ``polar()`` call will fail.
26932701
"""
26942702
# If an axis already exists, check if it has a polar projection
26952703
if gcf().get_axes():
26962704
ax = gca()
26972705
if not isinstance(ax, PolarAxes):
2698-
_api.warn_external('Trying to create polar plot on an Axes '
2699-
'that does not have a polar projection.')
2706+
_api.warn_deprecated(
2707+
"3.10",
2708+
message="There exists a non-polar current Axes. Therefore, the "
2709+
"resulting plot from 'polar()' is non-polar. You likely "
2710+
"should call 'polar()' before any other pyplot plotting "
2711+
"commands. "
2712+
"Support for this scenario is deprecated in %(since)s and "
2713+
"will raise an error in %(removal)s"
2714+
)
27002715
else:
27012716
ax = axes(projection="polar")
27022717
return ax.plot(*args, **kwargs)

0 commit comments

Comments
 (0)