Skip to content

Commit 386bb30

Browse files
committed
Apply padding to radial ticks in polar plots.
1 parent 8c430e8 commit 386bb30

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,58 @@ def view_limits(self, vmin, vmax):
370370
return mtransforms.nonsingular(min(0, vmin), vmax)
371371

372372

373+
class _ThetaShift(mtransforms.ScaledTranslation):
374+
"""
375+
Apply a padding shift based on axes theta limits.
376+
377+
This is used to create padding for radial ticks.
378+
379+
Parameters
380+
----------
381+
axes : matplotlib.axes.Axes
382+
The owning axes; used to determine limits.
383+
pad : float
384+
The padding to apply, in points.
385+
start : str, {'min', 'max', 'rlabel'}
386+
Whether to shift away from the start (``'min'``) or the end (``'max'``)
387+
of the axes, or using the rlabel position (``'rlabel'``).
388+
"""
389+
def __init__(self, axes, pad, mode):
390+
mtransforms.ScaledTranslation.__init__(self, pad, pad,
391+
axes.figure.dpi_scale_trans)
392+
self.set_children(axes._realViewLim)
393+
self.axes = axes
394+
self.mode = mode
395+
self.pad = pad
396+
397+
def get_matrix(self):
398+
if self._invalid:
399+
if self.mode == 'rlabel':
400+
angle = (
401+
np.deg2rad(self.axes.get_rlabel_position()) *
402+
self.axes.get_theta_direction() +
403+
self.axes.get_theta_offset()
404+
)
405+
else:
406+
if self.mode == 'min':
407+
angle = self.axes._realViewLim.xmin
408+
elif self.mode == 'max':
409+
angle = self.axes._realViewLim.xmax
410+
angle %= 2 * np.pi
411+
if angle < 0:
412+
angle += 2 * np.pi
413+
414+
if self.mode in ('rlabel', 'min'):
415+
padx = np.cos(angle - np.pi / 2)
416+
pady = np.sin(angle - np.pi / 2)
417+
else:
418+
padx = np.cos(angle + np.pi / 2)
419+
pady = np.sin(angle + np.pi / 2)
420+
421+
self._t = (self.pad * padx / 72, self.pad * pady / 72)
422+
return mtransforms.ScaledTranslation.get_matrix(self)
423+
424+
373425
class RadialTick(maxis.YTick):
374426
"""
375427
A radial-axis tick.
@@ -746,17 +798,25 @@ def get_yaxis_transform(self, which='grid'):
746798

747799
def get_yaxis_text1_transform(self, pad):
748800
thetamin, thetamax = self._realViewLim.intervalx
749-
full = _is_full_circle_rad(thetamin, thetamax)
750-
if self.get_theta_direction() > 0 or full:
751-
return self._yaxis_text_transform, 'center', 'left'
801+
if _is_full_circle_rad(thetamin, thetamax):
802+
halign = 'left'
803+
pad_shift = _ThetaShift(self, pad, 'rlabel')
804+
elif self.get_theta_direction() > 0:
805+
halign = 'left'
806+
pad_shift = _ThetaShift(self, pad, 'min')
752807
else:
753-
return self._yaxis_text_transform, 'center', 'right'
808+
halign = 'right'
809+
pad_shift = _ThetaShift(self, pad, 'max')
810+
return self._yaxis_text_transform + pad_shift, 'center', halign
754811

755812
def get_yaxis_text2_transform(self, pad):
756813
if self.get_theta_direction() > 0:
757-
return self._yaxis_text_transform, 'center', 'right'
814+
halign = 'right'
815+
pad_shift = _ThetaShift(self, pad, 'max')
758816
else:
759-
return self._yaxis_text_transform, 'center', 'left'
817+
halign = 'left'
818+
pad_shift = _ThetaShift(self, pad, 'min')
819+
return self._yaxis_text_transform + pad_shift, 'center', halign
760820

761821
def draw(self, *args, **kwargs):
762822
thetamin, thetamax = self._realViewLim.intervalx

0 commit comments

Comments
 (0)