8000 Apply padding to theta ticks in polar plots. · matplotlib/matplotlib@4ec0c49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ec0c49

Browse files
committed
Apply padding to theta ticks in polar plots.
1 parent d3c9e61 commit 4ec0c49

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77

8+
import matplotlib.artist as martist
89
from matplotlib.axes import Axes
910
import matplotlib.axis as maxis
1011
from matplotlib import cbook
@@ -248,8 +249,29 @@ class ThetaTick(maxis.XTick):
248249
This subclass of `XTick` provides angular ticks with some small
249250
modification to their re-positioning such that ticks are rotated based on
250251
tick location. This results in ticks that are correctly perpendicular to
251-
the arc spine. Labels are also rotated to be parallel to the spine.
252+
the arc spine.
253+
254+
Labels are also rotated to be parallel to the spine. The label padding is
255+
also applied here since it's not possible to use a generic axes transform
256+
to produce tick-specific padding.
252257
"""
258+
def __init__(self, axes, *args, **kwargs):
259+
maxis.XTick.__init__(self, axes, *args, **kwargs)
260+
self._text1_translate = mtransforms.ScaledTranslation(
261+
0, 0,
262+
axes.figure.dpi_scale_trans)
263+
self._text2_translate = mtransforms.ScaledTranslation(
264+
0, 0,
265+
axes.figure.dpi_scale_trans)
266+
267+
def _update_padding(self, angle):
268+
padx = self._pad * np.cos(angle) / 72
269+
pady = self._pad * np.sin(angle) / 72
270+
self._text1_translate._t = (padx, pady)
271+
self._text1_translate.invalidate()
272+
self._text2_translate._t = (-padx, -pady)
273+
self._text2_translate.invalidate()
274+
253275
def _get_text1(self):
254276
t = maxis.XTick._get_text1(self)
255277
t.set_rotation_mode('anchor')
@@ -292,6 +314,23 @@ def update_position(self, loc):
292314
if self.label2On:
293315
self.label2.set_rotation(np.rad2deg(angle) + self._labelrotation)
294316

317+
@martist.allow_rasterization
318+
def draw(self, renderer):
319+
axes = self.axes
320+
self._update_padding(self._loc * axes.get_theta_direction() +
321+
axes.get_theta_offset())
322+
323+
# Add the padding shift to the label. This can't be applied to the
324+
# transform by default because Axis likes to copy ticks/transforms,
325+
# leaving every one with the same padding direction.
326+
trans1 = self.label1.get_transform()
327+
self.label1.set_transform(trans1 + self._text1_translate)
328+
trans2 = self.label2.get_transform()
329+
self.label2.set_transform(trans2 + self._text2_translate)
330+
maxis.XTick.draw(self, renderer)
331+
self.label1.set_transform(trans1)
332+
self.label2.set_transform(trans2)
333+
295334

296335
class ThetaAxis(maxis.XAxis):
297336
"""
@@ -736,14 +775,7 @@ def _set_lim_and_transforms(self):
736775
.translate(0.0, -0.5) \
737776
.scale(1.0, -1.0) \
738777
.translate(0.0, 0.5)
739-
self._xaxis_text1_transform = (
740-
flipr_transform +
741-
mtransforms.Affine2D().translate(0.0, 0.1) +
742-
self._xaxis_transform)
743-
self._xaxis_text2_transform = (
744-
flipr_transform +
745-
mtransforms.Affine2D().translate(0.0, -0.1) +
746-
self._xaxis_transform)
778+
self._xaxis_text_transform = flipr_transform + self._xaxis_transform
747779

748780
# This is the transform for r-axis ticks. It scales the theta
749781
# axis so the gridlines from 0.0 to 1.0, now go from thetamin to
@@ -766,10 +798,10 @@ def get_xaxis_transform(self, which='grid'):
766798
return self._xaxis_transform
767799

768800
def get_xaxis_text1_transform(self, pad):
769-
return self._xaxis_text1_transform, 'bottom', 'center'
801+
return self._xaxis_text_transform, 'bottom', 'center'
770802

771803
def get_xaxis_text2_transform(self, pad):
772-
return self._xaxis_text2_transform, 'top', 'center'
804+
return self._xaxis_text_transform, 'top', 'center'
773805

774806
def get_yaxis_transform(self, which='grid'):
775807
if which in ('tick1', 'tick2'):

0 commit comments

Comments
 (0)
0