Skip to content

Backport PR #21131 on branch v3.5.x (Fix polar() regression on second call failure) #21138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2206,14 +2206,12 @@ def polar(*args, **kwargs):
# If an axis already exists, check if it has a polar projection
if gcf().get_axes():
ax = gca()
if isinstance(ax, PolarAxes):
return ax
else:
if not isinstance(ax, PolarAxes):
_api.warn_external('Trying to create polar plot on an Axes '
'that does not have a polar projection.')
ax = axes(projection="polar")
ret = ax.plot(*args, **kwargs)
return ret
else:
ax = axes(projection="polar")
return ax.plot(*args, **kwargs)


# If rcParams['backend_fallback'] is true, and an interactive backend is
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,13 @@ def test_subplot_change_projection():
assert ax_next.name == proj
assert ax is not ax_next
ax = ax_next


def test_polar_second_call():
# the first call creates the axes with polar projection
ln1, = plt.polar(0., 1., 'ro')
assert isinstance(ln1, mpl.lines.Line2D)
# the second call should reuse the existing axes
ln2, = plt.polar(1.57, .5, 'bo')
assert isinstance(ln2, mpl.lines.Line2D)
assert ln1.axes is ln2.axes