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

Skip to content

Commit 52ab847

Browse files
committed
FIX: Allow negative radii in RadialLocator and polar grids.
Polar axes do not have any issue with negative radii; the plotting axes are simply shifted so that the minimum radius of the data occurs at r=0 in plotting space. For all-positive radii, the RadialLocator will preferentially choose 0 as the minimum view limit to remain backwards compatible with the previous version. If any radii are negative, then the usual minimum is used.
1 parent 24cd817 commit 52ab847

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

lib/matplotlib/projections/polar.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,21 @@ class RadialLocator(Locator):
192192
:class:`~matplotlib.ticker.Locator` (which may be different
193193
depending on the scale of the *r*-axis.
194194
"""
195-
def __init__(self, base):
195+
def __init__(self, base, axes=None):
196196
self.base = base
197+
self._axes = axes
197198

198199
def __call__(self):
199-
ticks = self.base()
200-
return [x for x in ticks if x > 0]
200+
show_all = True
201+
# Ensure previous behaviour with full circle views.
202+
if self._axes:
203+
rmin = self._axes.get_rmin()
204+
show_all = False
205+
206+
if show_all:
207+
return self.base()
208+
else:
209+
return [tick for tick in self.base() if tick > rmin]
201210

202211
def autoscale(self):
203212
return self.base.autoscale()
@@ -213,7 +222,7 @@ def refresh(self):
213222

214223
def view_limits(self, vmin, vmax):
215224
vmin, vmax = self.base.view_limits(vmin, vmax)
216-
return 0, vmax
225+
return min(0, vmin), vmax
217226

218227

219228
class PolarAxes(Axes):
@@ -246,7 +255,8 @@ def cla(self):
246255
self.xaxis.isDefault_majfmt = True
247256
angles = np.arange(0.0, 360.0, 45.0)
248257
self.set_thetagrids(angles)
249-
self.yaxis.set_major_locator(self.RadialLocator(self.yaxis.get_major_locator()))
258+
self.yaxis.set_major_locator(
259+
self.RadialLocator(self.yaxis.get_major_locator(), self))
250260

251261
self.grid(rcParams['polaraxes.grid'])
252262
self.xaxis.set_ticks_position('none')
@@ -473,7 +483,7 @@ def set_rlabel_position(self, value):
473483
def set_yscale(self, *args, **kwargs):
474484
Axes.set_yscale(self, *args, **kwargs)
475485
self.yaxis.set_major_locator(
476-
self.RadialLocator(self.yaxis.get_major_locator()))
486+
self.RadialLocator(self.yaxis.get_major_locator(), self))
477487

478488
def set_rscale(self, *args, **kwargs):
479489
return Axes.set_yscale(self, *args, **kwargs)
@@ -549,9 +559,6 @@ def set_rgrids(self, radii, labels=None, angle=None, fmt=None,
549559
# Make sure we take into account unitized data
550560
radii = self.convert_xunits(radii)
551561
radii = np.asarray(radii)
552-
rmin = radii.min()
553-
if rmin <= 0:
554-
raise ValueError('radial grids must be strictly positive')
555562

556563
self.set_yticks(radii)
557564
if labels is not None:

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ def test_polar_rmin():
624624
ax.set_rmin(0.5)
625625

626626

627+
@image_comparison(baseline_images=['polar_negative_rmin'], style='default')
628+
def test_polar_negative_rmin():
629+
r = np.arange(-3.0, 0.0, 0.01)
630+
theta = 2*np.pi*r
631+
632+
fig = plt.figure()
633+
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
634+
ax.plot(theta, r)
635+
ax.set_rmax(0.0)
636+
ax.set_rmin(-3.0)
637+
638+
627639
@image_comparison(baseline_images=['polar_theta_position'])
628640
def test_polar_theta_position():
629641
r = np.arange(0, 3.0, 0.01)

0 commit comments

Comments
 (0)