From f238f1bfb088186569d5de801bc8fdf35ef5ed2d Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 8 Oct 2021 17:03:19 +0200 Subject: [PATCH 1/3] FIX: better error message for shared axes and axis('equal') --- lib/matplotlib/axes/_base.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 2f0f8865280f..42ebcd41a51e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1941,8 +1941,10 @@ def apply_aspect(self, position=None): shared_y = self in self._shared_axes["y"] # Not sure whether we need this check: if shared_x and shared_y: - raise RuntimeError("adjustable='datalim' is not allowed when both " - "axes are shared") + raise RuntimeError("set_aspect(..., adjustable='datalim') or " + "axis('equal') are not allowed when both axes " + "are shared. Try set_aspect(..., " + "adjustable='box').") # If y is shared, then we are only allowed to change x, etc. if shared_y: @@ -2044,7 +2046,11 @@ def axis(self, *args, emit=True, **kwargs): self.autoscale_view(tight=False) # self.apply_aspect() if s == 'equal': - self.set_aspect('equal', adjustable='datalim') + try: + self.set_aspect('equal', adjustable='datalim') + except RuntimeError: + raise RuntimeError("'equal' is not allowed on shared " + "axes, try 'scaled' instead.") elif s == 'scaled': self.set_aspect('equal', adjustable='box', anchor='C') self.set_autoscale_on(False) # Req. by Mark Bakker From 2ad34af140cf4798e2abf28ee1fa0492e1515a83 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 15 Oct 2021 16:47:55 +0200 Subject: [PATCH 2/3] TST: add test for error --- lib/matplotlib/tests/test_axes.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1b349b2a9c31..ef6164655122 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4938,6 +4938,13 @@ def test_shared_with_aspect_3(): assert round(expected, 4) == round(ax.get_aspect(), 4) +def test_shared_aspect_error(): + fig, axes = plt.subplots(1, 2, sharex=True, sharey=True) + axes[0].axis("equal") + with pytest.raises(RuntimeError, match=r"set_aspect\(..., adjustable="): + fig.draw_without_rendering() + + @pytest.mark.parametrize('twin', ('x', 'y')) def test_twin_with_aspect(twin): fig, ax = plt.subplots() From e07c28f6e6c1d892dd416515742a6bab3fcc83e8 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 23 Nov 2021 10:05:38 +0100 Subject: [PATCH 3/3] FIX: remove extra try/except --- lib/matplotlib/axes/_base.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 42ebcd41a51e..7941edc48179 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1939,7 +1939,7 @@ def apply_aspect(self, position=None): shared_x = self in self._shared_axes["x"] shared_y = self in self._shared_axes["y"] - # Not sure whether we need this check: + if shared_x and shared_y: raise RuntimeError("set_aspect(..., adjustable='datalim') or " "axis('equal') are not allowed when both axes " @@ -2044,13 +2044,8 @@ def axis(self, *args, emit=True, **kwargs): self.set_autoscale_on(True) self.set_aspect('auto') self.autoscale_view(tight=False) - # self.apply_aspect() if s == 'equal': - try: - self.set_aspect('equal', adjustable='datalim') - except RuntimeError: - raise RuntimeError("'equal' is not allowed on shared " - "axes, try 'scaled' instead.") + self.set_aspect('equal', adjustable='datalim') elif s == 'scaled': self.set_aspect('equal', adjustable='box', anchor='C') self.set_autoscale_on(False) # Req. by Mark Bakker