diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index d8d8313dbfc7..a076c7b4a15f 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -987,13 +987,30 @@ def set_thetalim(self, *args, **kwargs): where minval and maxval are the minimum and maximum limits. Values are wrapped in to the range :math:`[0, 2\pi]` (in radians), so for example it is possible to do ``set_thetalim(-np.pi / 2, np.pi / 2)`` to have - an axes symmetric around 0. + an axes symmetric around 0. A ValueError is raised if the absolute + angle difference is larger than :math:`2\pi`. """ + thetamin = None + thetamax = None + left = None + right = None + + if len(args) == 2: + if args[0] is not None and args[1] is not None: + left, right = args + if abs(right - left) > 2 * np.pi: + raise ValueError('The angle range must be <= 2 pi') + if 'thetamin' in kwargs: - kwargs['xmin'] = np.deg2rad(kwargs.pop('thetamin')) + thetamin = np.deg2rad(kwargs.pop('thetamin')) if 'thetamax' in kwargs: - kwargs['xmax'] = np.deg2rad(kwargs.pop('thetamax')) - return tuple(np.rad2deg(self.set_xlim(*args, **kwargs))) + thetamax = np.deg2rad(kwargs.pop('thetamax')) + + if thetamin is not None and thetamax is not None: + if abs(thetamax - thetamin) > 2 * np.pi: + raise ValueError('The angle range must be<= 360 degrees') + return tuple(np.rad2deg(self.set_xlim(left=left, right=right, + xmin=thetamin, xmax=thetamax))) def set_theta_offset(self, offset): """ diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index 782d56d736ae..6f44ef99b7f6 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -173,3 +173,26 @@ def test_dont_mutate_kwargs(): gridspec_kw=gridspec_kw) assert subplot_kw == {'sharex': 'all'} assert gridspec_kw == {'width_ratios': [1, 2]} + + +def test_subplot_theta_min_max_raise(): + with pytest.raises(ValueError, match='The angle range ' + + 'must be<= 360 degrees'): + ax = plt.subplot(111, projection='polar') + ax.set_thetalim(thetamin=800, thetamax=400) + + +def test_subplot_theta_min_max_non_raise(): + ax = plt.subplot(111, projection='polar') + ax.set_thetalim(thetamin=800, thetamax=440) + + +def test_subplot_theta_range_raise(): + with pytest.raises(ValueError, match='The angle range must be <= 2 pi'): + ax = plt.subplot(111, projection='polar') + ax.set_thetalim(0, 3 * numpy.pi) + + +def test_subplot_theta_range_normal_non_raise(): + ax = plt.subplot(111, projection='polar') + ax.set_thetalim(0, 2 * numpy.pi)