Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 651c748

Browse filesBrowse files
committed
Apply padding to theta ticks in polar plots.
1 parent 9facde5 commit 651c748
Copy full SHA for 651c748

File tree

Expand file treeCollapse file tree

1 file changed

+43
-11
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+43
-11
lines changed

‎lib/matplotlib/projections/polar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/projections/polar.py
+43-11Lines 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
@@ -378,8 +379,29 @@ class _ThetaTick(maxis.XTick):
378379
This subclass of `XTick` provides angular ticks with some small
379380
modification to their re-positioning such that ticks are rotated based on
380381
tick location. This results in ticks that are correctly perpendicular to
381-
the arc spine. Labels are also rotated to be parallel to the spine.
382+
the arc spine.
383+
384+
Labels are also rotated to be parallel to the spine. The label padding is
385+
also applied here since it's not possible to use a generic axes transform
386+
to produce tick-specific padding.
382387
"""
388+
def __init__(self, axes, *args, **kwargs):
389+
maxis.XTick.__init__(self, axes, *args, **kwargs)
390+
self._text1_translate = mtransforms.ScaledTranslation(
391+
0, 0,
392+
axes.figure.dpi_scale_trans)
393+
self._text2_translate = mtransforms.ScaledTranslation(
394+
0, 0,
395+
axes.figure.dpi_scale_trans)
396+
397+
def _update_padding(self, angle):
398+
padx = self._pad * np.cos(angle) / 72
399+
pady = self._pad * np.sin(angle) / 72
400+
self._text1_translate._t = (padx, pady)
401+
self._text1_translate.invalidate()
402+
self._text2_translate._t = (-padx, -pady)
403+
self._text2_translate.invalidate()
404+
383405
def _get_text1(self):
384406
t = maxis.XTick._get_text1(self)
385407
t.set_rotation_mode('anchor')
@@ -422,6 +444,23 @@ def update_position(self, loc):
422444
if self.label2On:
423445
self.label2.set_rotation(np.rad2deg(angle) + self._labelrotation)
424446

447+
@martist.allow_rasterization
448+
def draw(self, renderer):
449+
axes = self.axes
450+
self._update_padding(self._loc * axes.get_theta_direction() +
451+
axes.get_theta_offset())
452+
453+
# Add the padding shift to the label. This can't be applied to the
454+
# transform by default because Axis likes to copy ticks/transforms,
455+
# leaving every one with the same padding direction.
456+
trans1 = self.label1.get_transform()
457+
self.label1.set_transform(trans1 + self._text1_translate)
458+
trans2 = self.label2.get_transform()
459+
self.label2.set_transform(trans2 + self._text2_translate)
460+
maxis.XTick.draw(self, renderer)
461+
self.label1.set_transform(trans1)
462+
self.label2.set_transform(trans2)
463+
425464

426465
class _ThetaAxis(maxis.XAxis):
427466
"""
@@ -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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.