From 1ba67be8712222a3ade32b2a13443921fb947e07 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 20 Sep 2021 09:46:39 +0200 Subject: [PATCH] Backport PR #21131: Fix polar() regression on second call failure --- lib/matplotlib/pyplot.py | 10 ++++------ lib/matplotlib/tests/test_pyplot.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 35579711ede0..a203984aacec 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -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 diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index c2c71d586715..d0d38c78ed7f 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -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