Skip to content

Commit 2d28b7f

Browse files
committed
ENH: Add PolarAxes.get_rlim() and get_thetalim() for API symmetry
1 parent e9637a6 commit 2d28b7f

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
``PolarAxes.get_rlim()`` and ``get_thetalim()`` added
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
:class:`~matplotlib.projections.polar.PolarAxes` now provides
5+
`~matplotlib.projections.polar.PolarAxes.get_rlim` and
6+
`~matplotlib.projections.polar.PolarAxes.get_thetalim` to complement the
7+
existing `~matplotlib.projections.polar.PolarAxes.set_rlim` and
8+
`~matplotlib.projections.polar.PolarAxes.set_thetalim`. Previously, calling
9+
these getters raised an ``AttributeError``; the workaround was to call the
10+
base-class ``get_ylim()`` / ``get_xlim()`` directly::
11+
12+
ax = plt.subplot(projection="polar")
13+
ax.set_rlim(1, 5)
14+
rmin, rmax = ax.get_rlim() # was: AttributeError

lib/matplotlib/projections/polar.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,21 @@ def set_thetalim(self, *args, **kwargs):
10651065
raise ValueError("The angle range must be less than a full circle")
10661066
return tuple(np.rad2deg((new_min, new_max)))
10671067

1068+
def get_thetalim(self):
1069+
"""
1070+
Get the minimum and maximum theta values.
1071+
1072+
Returns
1073+
-------
1074+
thetamin, thetamax : float
1075+
The minimum and maximum theta limit values in radians.
1076+
1077+
See Also
1078+
--------
1079+
set_thetalim
1080+
"""
1081+
return self.get_xlim()
1082+
10681083
def set_theta_offset(self, offset):
10691084
"""
10701085
Set the offset for the location of 0 in radians.
@@ -1224,6 +1239,21 @@ def set_rlim(self, bottom=None, top=None, *,
12241239
return self.set_ylim(bottom=bottom, top=top, emit=emit, auto=auto,
12251240
**kwargs)
12261241

1242+
def get_rlim(self):
1243+
"""
1244+
Get the radial axis view limits.
1245+
1246+
Returns
1247+
-------
1248+
bottom, top : float
1249+
The lower and upper radial axis limits.
1250+
1251+
See Also
1252+
--------
1253+
set_rlim
1254+
"""
1255+
return self.get_ylim()
1256+
12271257
def get_rlabel_position(self):
12281258
"""
12291259
Returns

lib/matplotlib/tests/test_polar.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,29 @@ def test_axvspan():
398398
assert span.get_path()._interpolation_steps > 1
399399

400400

401+
def test_polar_get_rlim():
402+
# GH#31694 — PolarAxes.get_rlim() should mirror set_rlim()
403+
ax = plt.figure().add_subplot(projection='polar')
404+
ax.set_rlim(1.5, 8.0)
405+
assert ax.get_rlim() == (1.5, 8.0)
406+
407+
408+
def test_polar_get_rlim_after_plot():
409+
# GH#31694 — get_rlim() should work after autoscaling via plot()
410+
ax = plt.figure().add_subplot(projection='polar')
411+
theta = np.linspace(0, 2 * np.pi, 10)
412+
ax.plot(theta, np.ones(10) * 5.0)
413+
rmin, rmax = ax.get_rlim()
414+
assert rmax >= 5.0
415+
416+
417+
def test_polar_get_thetalim():
418+
# GH#31694 — PolarAxes.get_thetalim() should mirror set_thetalim()
419+
ax = plt.figure().add_subplot(projection='polar')
420+
ax.set_thetalim(0.5, 2.7)
421+
assert_allclose(ax.get_thetalim(), (0.5, 2.7))
422+
423+
401424
@check_figures_equal()
402425
def test_remove_shared_polar(fig_ref, fig_test):
403426
# Removing shared polar axes used to crash. Test removing them, keeping in

0 commit comments

Comments
 (0)