Skip to content

Polar limits enhancements #4699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
QuLogic committed Aug 16, 2017
commit 52ab847fb6c3ffe1ac264e311ed321b52c3aaa74
25 changes: 16 additions & 9 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,21 @@ class RadialLocator(Locator):
:class:`~matplotlib.ticker.Locator` (which may be different
depending on the scale of the *r*-axis.
"""
def __init__(self, base):
def __init__(self, base, axes=None):
self.base = base
self._axes = axes

def __call__(self):
ticks = self.base()
return [x for x in ticks if x > 0]
show_all = True
# Ensure previous behaviour with full circle views.
if self._axes:
rmin = self._axes.get_rmin()
show_all = False

if show_all:
return self.base()
else:
return [tick for tick in self.base() if tick > rmin]

def autoscale(self):
return self.base.autoscale()
Expand All @@ -213,7 +222,7 @@ def refresh(self):

def view_limits(self, vmin, vmax):
vmin, vmax = self.base.view_limits(vmin, vmax)
return 0, vmax
return min(0, vmin), vmax


class PolarAxes(Axes):
Expand Down Expand Up @@ -246,7 +255,8 @@ def cla(self):
self.xaxis.isDefault_majfmt = True
angles = np.arange(0.0, 360.0, 45.0)
self.set_thetagrids(angles)
self.yaxis.set_major_locator(self.RadialLocator(self.yaxis.get_major_locator()))
self.yaxis.set_major_locator(
self.RadialLocator(self.yaxis.get_major_locator(), self))

self.grid(rcParams['polaraxes.grid'])
self.xaxis.set_ticks_position('none')
Expand Down Expand Up @@ -473,7 +483,7 @@ def set_rlabel_position(self, value):
def set_yscale(self, *args, **kwargs):
Axes.set_yscale(self, *args, **kwargs)
self.yaxis.set_major_locator(
self.RadialLocator(self.yaxis.get_major_locator()))
self.RadialLocator(self.yaxis.get_major_locator(), self))

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

self.set_yticks(radii)
if labels is not None:
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,18 @@ def test_polar_rmin():
ax.set_rmin(0.5)


@image_comparison(baseline_images=['polar_negative_rmin'], style='default')
def test_polar_negative_rmin():
r = np.arange(-3.0, 0.0, 0.01)
theta = 2*np.pi*r

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
ax.plot(theta, r)
ax.set_rmax(0.0)
ax.set_rmin(-3.0)


@image_comparison(baseline_images=['polar_theta_position'])
def test_polar_theta_position():
r = np.arange(0, 3.0, 0.01)
Expand Down