Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 316c3bf

Browse files
authored
Merge pull request #19987 from anntzer/thetaparse
Fix set_thetalim((min, max)).
2 parents 5799a5c + 2431ecb commit 316c3bf

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,29 +1041,20 @@ def set_thetalim(self, *args, **kwargs):
10411041
wrapped in to the range :math:`[0, 2\pi]` (in radians), so for example
10421042
it is possible to do ``set_thetalim(-np.pi / 2, np.pi / 2)`` to have
10431043
an axes symmetric around 0. A ValueError is raised if the absolute
1044-
angle difference is larger than :math:`2\pi`.
1044+
angle difference is larger than a full circle.
10451045
"""
1046-
thetamin = None
1047-
thetamax = None
1048-
left = None
1049-
right = None
1050-
1051-
if len(args) == 2:
1052-
if args[0] is not None and args[1] is not None:
1053-
left, right = args
1054-
if abs(right - left) > 2 * np.pi:
1055-
raise ValueError('The angle range must be <= 2 pi')
1056-
1046+
orig_lim = self.get_xlim() # in radians
10571047
if 'thetamin' in kwargs:
1058-
thetamin = np.deg2rad(kwargs.pop('thetamin'))
1048+
kwargs['xmin'] = np.deg2rad(kwargs.pop('thetamin'))
10591049
if 'thetamax' in kwargs:
1060-
thetamax = np.deg2rad(kwargs.pop('thetamax'))
1061-
1062-
if thetamin is not None and thetamax is not None:
1063-
if abs(thetamax - thetamin) > 2 * np.pi:
1064-
raise ValueError('The angle range must be <= 360 degrees')
1065-
return tuple(np.rad2deg(self.set_xlim(left=left, right=right,
1066-
xmin=thetamin, xmax=thetamax)))
1050+
kwargs['xmax'] = np.deg2rad(kwargs.pop('thetamax'))
1051+
new_min, new_max = self.set_xlim(*args, **kwargs)
1052+
# Parsing all permutations of *args, **kwargs is tricky; it is simpler
1053+
# to let set_xlim() do it and then validate the limits.
1054+
if abs(new_max - new_min) > 2 * np.pi:
1055+
self.set_xlim(orig_lim) # un-accept the change
1056+
raise ValueError("The angle range must be less than a full circle")
1057+
return tuple(np.rad2deg((new_min, new_max)))
10671058

10681059
def set_theta_offset(self, offset):
10691060
"""

lib/matplotlib/tests/test_polar.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,17 @@ def test_thetalim_valid_invalid():
343343
ax = plt.subplot(projection='polar')
344344
ax.set_thetalim(0, 2 * np.pi) # doesn't raise.
345345
ax.set_thetalim(thetamin=800, thetamax=440) # doesn't raise.
346-
with pytest.raises(ValueError, match='The angle range must be <= 2 pi'):
346+
with pytest.raises(ValueError,
347+
match='angle range must be less than a full circle'):
347348
ax.set_thetalim(0, 3 * np.pi)
348349
with pytest.raises(ValueError,
349-
match='The angle range must be <= 360 degrees'):
350+
match='angle range must be less than a full circle'):
350351
ax.set_thetalim(thetamin=800, thetamax=400)
352+
353+
354+
def test_thetalim_args():
355+
ax = plt.subplot(projection='polar')
356+
ax.set_thetalim(0, 1)
357+
assert tuple(np.radians((ax.get_thetamin(), ax.get_thetamax()))) == (0, 1)
358+
ax.set_thetalim((2, 3))
359+
assert tuple(np.radians((ax.get_thetamin(), ax.get_thetamax()))) == (2, 3)

0 commit comments

Comments
 (0)