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 d8fa323

Browse filesBrowse files
committed
Add an 'auto' option to label rotation.
This option enables the automatic rotation of polar tick labels.
1 parent 67b86c6 commit d8fa323
Copy full SHA for d8fa323

File tree

Expand file treeCollapse file tree

4 files changed

+49
-12
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+49
-12
lines changed

‎doc/users/whats_new.rst

Copy file name to clipboardExpand all lines: doc/users/whats_new.rst
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ negative values are simply used as labels, and the real radius is shifted by
6262
the configured minimum. This release also allows negative radii to be used for
6363
grids and ticks, which were previously silently ignored.
6464

65-
For plots of a partial circle, radial ticks and tick labels have been modified
66-
to be parallel to the circular grid line. Angular ticks have been modified to
67-
be parallel to the grid line and the labels are now perpendicular to the grid
68-
line (i.e., parallel to the outer boundary.)
65+
Radial ticks have be modified to be parallel to the circular grid line. Angular
66+
ticks will be modified to be parallel to the grid line. It may also be useful
67+
to rotate tick labels to match the boundary. Calling
68+
``ax.tick_params(rotation='auto')`` will enable this new behavior. Radial tick
69+
labels will be modified to be parallel to the circular grid line. Angular tick
70+
labels will be perpendicular to the grid line (i.e., parallel to the outer
71+
boundary.)
6972

7073

7174
Merge JSAnimation

‎lib/matplotlib/axis.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axis.py
+22-2Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def __init__(self, axes, loc, label,
140140
labelsize = rcParams['%s.labelsize' % name]
141141
self._labelsize = labelsize
142142

143-
self._labelrotation = labelrotation
143+
self._set_labelrotation(labelrotation)
144144

145145
if zorder is None:
146146
if major:
@@ -167,6 +167,20 @@ def __init__(self, axes, loc, label,
167167

168168
self.update_position(loc)
169169

170+
def _set_labelrotation(self, labelrotation):
171+
if isinstance(labelrotation, six.string_types):
172+
mode = labelrotation
173+
angle = 0
174+
elif isinstance(labelrotation, (tuple, list)):
175+
mode, angle = labelrotation
176+
else:
177+
mode = 'default'
178+
angle = labelrotation
179+
if mode not in ('auto', 'default'):
180+
raise ValueError("Label rotation mode must be 'default' or "
181+
"'auto', not '{}'.".format(mode))
182+
self._labelrotation = (mode, angle)
183+
170184
def apply_tickdir(self, tickdir):
171185
"""
172186
Calculate self._pad and self._tickmarkers
@@ -331,8 +345,14 @@ def _apply_params(self, **kw):
331345
self.tick2line.set(**tick_kw)
332346
for k, v in six.iteritems(tick_kw):
333347
setattr(self, '_' + k, v)
348+
349+
if 'labelrotation' in kw:
350+
self._set_labelrotation(kw.pop('labelrotation'))
351+
self.label1.set(rotation=self._labelrotation[1])
352+
self.label2.set(rotation=self._labelrotation[1])
353+
334354
label_list = [k for k in six.iteritems(kw)
335-
if k[0] in ['labelsize', 'labelcolor', 'labelrotation']]
355+
if k[0] in ['labelsize', 'labelcolor']]
336356
if label_list:
337357
label_kw = {k[5:]: v for k, v in label_list}
338358
self.label1.set(**label_kw)

‎lib/matplotlib/projections/polar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/projections/polar.py
+17-4Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4+
import six
5+
46
from collections import OrderedDict
57

68
import numpy as np
@@ -322,14 +324,17 @@ def update_position(self, loc):
322324
trans = self.tick2line._marker._transform
323325
self.tick2line._marker._transform = trans
324326

325-
if not _is_full_circle_deg(axes.get_thetamin(), axes.get_thetamax()):
327+
mode, user_angle = self._labelrotation
328+
if mode == 'default':
329+
angle = 0
330+
elif not _is_full_circle_deg(axes.get_thetamin(), axes.get_thetamax()):
326331
if angle > np.pi / 2:
327332
angle -= np.pi
328333
elif angle < -np.pi / 2:
329334
angle += np.pi
330335
else:
331336
angle = 0
332-
angle = np.rad2deg(angle) + self._labelrotation
337+
angle = np.rad2deg(angle) + user_angle
333338
if self.label1On:
334339
self.label1.set_rotation(angle)
335340
if self.label2On:
@@ -539,7 +544,11 @@ def update_position(self, loc):
539544
text_angle = angle + 180
540545
else:
541546
text_angle = angle
542-
text_angle += self._labelrotation
547+
mode, user_angle = self._labelrotation
548+
if mode == 'auto':
549+
text_angle += user_angle
550+
else:
551+
text_angle = user_angle
543552
if self.label1On:
544553
if full:
545554
ha = 'left'
@@ -583,7 +592,11 @@ def update_position(self, loc):
583592
text_angle = angle + 180
584593
else:
585594
text_angle = angle
586-
text_angle += self._labelrotation
595+
mode, user_angle = self._labelrotation
596+
if mode == 'auto':
597+
text_angle += user_angle
598+
else:
599+
text_angle = user_angle
587600
if self.label2On:
588601
ha, va = self._determine_anchor(angle, False)
589602
self.label2.set_ha(ha)

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,9 @@ def test_polar_theta_limits():
692692
ax.set_thetamin(start)
693693
ax.set_thetamax(end)
694694
ax.tick_params(tick1On=True, tick2On=True,
695-
direction=DIRECTIONS[i % len(DIRECTIONS)])
696-
ax.yaxis.set_tick_params(label2On=True)
695+
direction=DIRECTIONS[i % len(DIRECTIONS)],
696+
rotation='auto')
697+
ax.yaxis.set_tick_params(label2On=True, rotation='auto')
697698
else:
698699
ax.set_visible(False)
699700

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.