From 88a44248d4159cbd32f12364e7236feb18c02481 Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Thu, 20 Mar 2025 22:28:28 +0000 Subject: [PATCH 1/8] Fix #29528 set_rticks makes autoscale move the origin away from zero After calling `set_rticks`, the lower radial limit was being overwritten when `plot` was called. This was due to `autoscale_view` logic not enforcing the constraint that the lower radial limit must be 0 in polar plots. This fix ensures that the lower radial limit is always enforced by explicitly setting it to 0 inside `handle_single_axis` (inside `autoscale_view`). Added `test_radial_limit_after_plot` to `test_polar.py` to assert that the lower limit of the radial axis is 0. --- lib/matplotlib/axes/_base.py | 4 ++++ lib/matplotlib/tests/test_polar.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 4b41cf462f80..266b4dc8220f 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3056,6 +3056,10 @@ def handle_single_axis( if not self._tight: x0, x1 = locator.view_limits(x0, x1) + + # Enforce lower radial limit of 0 for polar plots + if name == 'y' and self.name == 'polar': + x0 = 0 set_bound(x0, x1) # End of definition of internal function 'handle_single_axis'. diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index ff2ad6433e6b..96394d04c731 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -506,3 +506,11 @@ def test_polar_errorbar(order): ax.errorbar(theta, r, xerr=0.1, yerr=0.1, capsize=7, fmt="o", c="seagreen") ax.set_theta_zero_location("N") ax.set_theta_direction(-1) + + +def test_radial_limit_after_plot(): + fig = plt.figure() + ax = fig.add_subplot(projection='polar') + ax.set_rticks([1, 2, 3, 4]) + ax.plot([0, 1], [2, 3]) + assert ax.get_ylim()[0] == (0) From 273dff3396fd69e08a5bcbb8706bd1e33c97cdce Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Tue, 25 Mar 2025 14:09:43 +0000 Subject: [PATCH 2/8] Fix #29528 set_rticks makes autoscale move the origin away from 0. After calling `set_rticks`, the lower radial limit was being overwritten when `plot` was called. This was due to `set_rticks` overwriting the `RadialLocator` wrapper for a `FixedLocator` losing all Radial logic. This fix sets the RadialLocator inside `set_rticks` after calling `Axes.set_yticks` ensuring all radial logic is applied. Added `test_radial_limits_behavior` to `test_polar.py` to assert that the lower limit of the radial axis is maintained after `set_rticks`. --- lib/matplotlib/projections/polar.py | 4 +++- lib/matplotlib/tests/test_polar.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index 7fe6045039b1..05d84b078667 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -1292,7 +1292,9 @@ def set_rscale(self, *args, **kwargs): return Axes.set_yscale(self, *args, **kwargs) def set_rticks(self, *args, **kwargs): - return Axes.set_yticks(self, *args, **kwargs) + Axes.set_yticks(self, *args, **kwargs) + self.yaxis.set_major_locator( + self.RadialLocator(self.yaxis.get_major_locator(), self)) def set_thetagrids(self, angles, labels=None, fmt=None, **kwargs): """ diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index 96394d04c731..3482cf7e2bf0 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -508,9 +508,10 @@ def test_polar_errorbar(order): ax.set_theta_direction(-1) -def test_radial_limit_after_plot(): +def test_radial_limits_behavior(): fig = plt.figure() ax = fig.add_subplot(projection='polar') ax.set_rticks([1, 2, 3, 4]) - ax.plot([0, 1], [2, 3]) assert ax.get_ylim()[0] == (0) + ax.plot([0, 1], [2, 3]) + assert ax.get_ylim()[0] == 0 From c142cfaa7a9623688829a16ca363c61125add8cc Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Tue, 25 Mar 2025 14:16:25 +0000 Subject: [PATCH 3/8] Fix #29528 set_rticks makes autoscale move the origin away from 0. After calling `set_rticks`, the lower radial limit was being overwritten when `plot` was called. This was due to `set_rticks` overwriting the `RadialLocator` wrapper for a `FixedLocator` losing all Radial logic. This fix sets the RadialLocator inside `set_rticks` after calling `Axes.set_yticks` ensuring all radial logic is applied. Added `test_radial_limits_behavior` to `test_polar.py` to assert that the lower limit of the radial axis is maintained after `set_rticks`. --- lib/matplotlib/axes/_base.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 266b4dc8220f..4b41cf462f80 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3056,10 +3056,6 @@ def handle_single_axis( if not self._tight: x0, x1 = locator.view_limits(x0, x1) - - # Enforce lower radial limit of 0 for polar plots - if name == 'y' and self.name == 'polar': - x0 = 0 set_bound(x0, x1) # End of definition of internal function 'handle_single_axis'. From e9817d21f556672af1ef2a5e889528582e331639 Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Tue, 25 Mar 2025 16:07:53 +0000 Subject: [PATCH 4/8] Update lib/matplotlib/tests/test_polar.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_polar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index 3482cf7e2bf0..d7699e44bbbb 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -512,6 +512,6 @@ def test_radial_limits_behavior(): fig = plt.figure() ax = fig.add_subplot(projection='polar') ax.set_rticks([1, 2, 3, 4]) - assert ax.get_ylim()[0] == (0) + assert ax.get_ylim()[0] == 0 ax.plot([0, 1], [2, 3]) assert ax.get_ylim()[0] == 0 From faa05155347c771199b6278df71bb8a83a6f7f89 Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Tue, 25 Mar 2025 19:37:00 +0000 Subject: [PATCH 5/8] Preserve set_yticks return value --- lib/matplotlib/projections/polar.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index 05d84b078667..71224fb3affe 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -1292,9 +1292,10 @@ def set_rscale(self, *args, **kwargs): return Axes.set_yscale(self, *args, **kwargs) def set_rticks(self, *args, **kwargs): - Axes.set_yticks(self, *args, **kwargs) + result = Axes.set_yticks(self, *args, **kwargs) self.yaxis.set_major_locator( self.RadialLocator(self.yaxis.get_major_locator(), self)) + return result def set_thetagrids(self, angles, labels=None, fmt=None, **kwargs): """ From 42f1d0b7401d69b179350f255803e8082e2dde89 Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Sat, 29 Mar 2025 13:57:34 +0000 Subject: [PATCH 6/8] Update lib/matplotlib/tests/test_polar.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_polar.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index d7699e44bbbb..2458f01b91a9 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -509,9 +509,20 @@ def test_polar_errorbar(order): def test_radial_limits_behavior(): + # r=0 is kept as limit if positive data and ticks are used + # negative ticks or data result in negativ limits fig = plt.figure() ax = fig.add_subplot(projection='polar') + assert ax.get_ylim() == (0, 1) + # upper limit is expanded to include the ticks, but lower limit stays at 0 ax.set_rticks([1, 2, 3, 4]) - assert ax.get_ylim()[0] == 0 - ax.plot([0, 1], [2, 3]) - assert ax.get_ylim()[0] == 0 + assert ax.get_ylim() == (0, 4) + # upper limit is autoscaled to data, but lower limit limit stays 0 + ax.plot([1, 2], [1, 2]) + assert ax.get_ylim() == (0, 2.05) + # negative ticks also expand the negative limit + ax.set_rticks([-1, 0, 1, 2]) + assert ax.get_ylim() == (-1, 2.05) + # negative data also autoscales to negative limits + ax.plot([1, 2], [-1, -2]) + ax.get_ylim() == (-2.2, 2.2) From 2d7541c50b9ef2f9246cce8dcc58b55b2c78a43f Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Sat, 29 Mar 2025 14:35:49 +0000 Subject: [PATCH 7/8] fixed failing checks --- lib/matplotlib/tests/test_polar.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index 2458f01b91a9..f0ad22e1b599 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -510,7 +510,7 @@ def test_polar_errorbar(order): def test_radial_limits_behavior(): # r=0 is kept as limit if positive data and ticks are used - # negative ticks or data result in negativ limits + # negative ticks or data result in negative limits fig = plt.figure() ax = fig.add_subplot(projection='polar') assert ax.get_ylim() == (0, 1) @@ -519,10 +519,10 @@ def test_radial_limits_behavior(): assert ax.get_ylim() == (0, 4) # upper limit is autoscaled to data, but lower limit limit stays 0 ax.plot([1, 2], [1, 2]) - assert ax.get_ylim() == (0, 2.05) + assert ax.get_ylim() == (0, 2) # negative ticks also expand the negative limit ax.set_rticks([-1, 0, 1, 2]) - assert ax.get_ylim() == (-1, 2.05) + assert ax.get_ylim() == (-1, 2) # negative data also autoscales to negative limits ax.plot([1, 2], [-1, -2]) ax.get_ylim() == (-2.2, 2.2) From 52107b84404086ec11f98de89dd331c9d2a95bcc Mon Sep 17 00:00:00 2001 From: Tiago Marques Date: Sat, 29 Mar 2025 19:06:26 +0000 Subject: [PATCH 8/8] Account for 0 margins in tests Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com> --- lib/matplotlib/tests/test_polar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index f0ad22e1b599..27bcd3fa11a8 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -525,4 +525,4 @@ def test_radial_limits_behavior(): assert ax.get_ylim() == (-1, 2) # negative data also autoscales to negative limits ax.plot([1, 2], [-1, -2]) - ax.get_ylim() == (-2.2, 2.2) + assert ax.get_ylim() == (-2, 2) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy